Help me with arrays.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bryon
    New Member
    • Apr 2010
    • 12

    Help me with arrays.....

    I have a homework problem for my C programing class but I am so lost.....

    The question is:

    We have two arrays A and B, each of 10 integers. Write a function that tests if every element of array A is equal to its corresponding element in array B. In other words, the function must check if A[0] is equal to B[0], A[1] is equal to B[1], and so forth. The function is to return true if all elements are equal and false if at least one elements is not equal.

    Any help on how to get this to work would be soooooooo appreciated. I have been trying for about a week and cant get anything to work.
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Post your code between code tags and tell us where you are having difficulty.
    This is a relatively simple exercise where two arrays of the same size are compared one element at a time (using a for or while loop) to establish if they are identical and print true or false.

    Comment

    • bryon
      New Member
      • Apr 2010
      • 12

      #3
      well here is what I have so far... I just don't understand how you get them to compare to each other. I have read the chapter in my book over and over but it makes no sense to me.

      [code=c]
      #include "stdafx.h"

      #define ARY_SIZE 10

      int main(void)
      {
      printf ("Welcome please enjoy this program.\n\n");

      int a [ARY_SIZE];
      int b [ARY_SIZE];

      printf ("Enter 10 numbers for line A.\n");
      for (int i = 0; i < 10; i++)
      scanf("%d", &a [i]);


      printf ("\nEnter 10 numbers for line B.\n");
      for (int i = 0; i < 10; i++)
      scanf("%d", &b [i]);



      printf ("\nThe lines match: \n");

      return 0;
      }
      [/code]

      Comment

      • bryon
        New Member
        • Apr 2010
        • 12

        #4
        Ok I have gotten more down but now i'm having a problem creating a loop to return true or false if the elements match or not.

        [code=c]
        #include "stdafx.h"

        #define ARY_SIZE 3

        int main(void)
        {
        printf ("Welcome please enjoy this program.\n\n");

        int a [ARY_SIZE];
        int b [ARY_SIZE];

        printf ("Enter 10 numbers for array A.\n");
        for (int i = 0; i < ARY_SIZE; i++)
        scanf("%d", &a [i]);


        printf ("\nEnter 10 numbers for array B.\n");
        for (int i = 0; i < ARY_SIZE; i++)
        scanf("%d", &b [i]);


        printf ("\n\nArray A: ");
        for(int i = 0; i < ARY_SIZE; i++)
        printf("%d ", a [i]);
        printf("\n");

        printf ("\nArray B: ");
        for(int i = 0; i < ARY_SIZE; i++)
        printf("%d ", b [i]);
        printf("\n");


        printf ("\nThe lines match: \n");
        //this is where I need a loop to return "true" or "false"




        return 0;
        }
        [/code]

        Comment

        • bryon
          New Member
          • Apr 2010
          • 12

          #5
          !!!!!!!!!!!!!!! !!! Some how I got it to work!! Its not the cleanest code ever but it works!! If you have any ideas on how you could of made it simpler or better ideas please tell me. I would like to learn how I could of made it easier on myself.


          Here is my code:

          [code=c]
          #include "stdafx.h"

          #define ARY_SIZE 10

          int main(void)
          {
          printf ("Welcome please enjoy this program.\n\n");

          int a [ARY_SIZE];
          int b [ARY_SIZE];

          printf ("Enter 10 numbers for array A.\n");
          for (int i = 0; i < ARY_SIZE; i++)
          scanf("%d", &a [i]);


          printf ("\nEnter 10 numbers for array B.\n");
          for (int i = 0; i < ARY_SIZE; i++)
          scanf("%d", &b [i]);


          printf ("\n\nArray A: ");
          for(int i = 0; i < ARY_SIZE; i++)
          printf("%d ", a [i]);
          printf("\n");

          printf ("\nArray B: ");
          for(int i = 0; i < ARY_SIZE; i++)
          printf("%d ", b [i]);
          printf("\n");


          printf ("\nThe arrays match:");
          for (int i = 0; i < 1; i++)
          {
          if (a[0] != b[0])
          printf("False\n \n");
          else if (a[1] != b[1])
          printf("False\n \n");
          else if (a[2] != b[2])
          printf("False\n \n");
          else if (a[3] != b[3])
          printf("False\n \n");
          else if (a[4] != b[4])
          printf("False\n \n");
          else if (a[5] != b[5])
          printf("False\n \n");
          else if (a[6] != b[6])
          printf("False\n \n");
          else if (a[7] != b[7])
          printf("False\n \n");
          else if (a[8] != b[8])
          printf("False\n \n");
          else if (a[9] != b[9])
          printf("False\n \n");
          else
          printf("True\n\ n");
          }





          return 0;
          }
          [/code]

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Two methods for doing this more cleanly, the flag method and the break method.

            Flag:
            Code:
            bool match = true; // assume they match beforehand
               for (int i = 0; i < 10; i++) 
                { 
                    if (a[i] != b[i]) 
                    {
                        match = false;
                    }
                }
            printf(match);
            break method. (you'd probably lose marks if you submitted this though).
            Code:
               for (int i = 0; i < 10; i++) 
                { 
                    if (a[i] != b[i]) 
                         break;
                }
               if (i<10) printf("false");  // i<10 only if we used break to exit the loop.
               else print("true");

            Comment

            • bryon
              New Member
              • Apr 2010
              • 12

              #7
              I like the second one. I dont think I would lose points cause we learned about breaks but I just never used one before. The first one kind of confuses me.

              Comment

              Working...