473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help me with arrays.....

12
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.
Apr 6 '10 #1

✓ answered by jkmyoung

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

Flag:
Expand|Select|Wrap|Line Numbers
  1. bool match = true; // assume they match beforehand
  2.    for (int i = 0; i < 10; i++) 
  3.     { 
  4.         if (a[i] != b[i]) 
  5.         {
  6.             match = false;
  7.         }
  8.     }
  9. printf(match); 
break method. (you'd probably lose marks if you submitted this though).
Expand|Select|Wrap|Line Numbers
  1.    for (int i = 0; i < 10; i++) 
  2.     { 
  3.         if (a[i] != b[i]) 
  4.              break;
  5.     }
  6.    if (i<10) printf("false");  // i<10 only if we used break to exit the loop.
  7.    else print("true");

6 4768
whodgson
542 512MB
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.
Apr 7 '10 #2
bryon
12
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.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #define ARY_SIZE 10
  4.  
  5. int main(void)
  6. {
  7.     printf ("Welcome please enjoy this program.\n\n");
  8.  
  9.     int a [ARY_SIZE];
  10.     int b [ARY_SIZE];
  11.  
  12.     printf ("Enter 10 numbers for line A.\n");
  13.     for (int i = 0; i < 10; i++)
  14.         scanf("%d", &a [i]);
  15.  
  16.  
  17.     printf ("\nEnter 10 numbers for line B.\n");
  18.     for (int i = 0; i < 10; i++)
  19.         scanf("%d", &b [i]);
  20.  
  21.  
  22.  
  23.     printf ("\nThe lines match: \n");
  24.  
  25.     return 0;
  26. }
  27.  
Apr 7 '10 #3
bryon
12
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.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #define ARY_SIZE 3
  4.  
  5. int main(void)
  6. {
  7.     printf ("Welcome please enjoy this program.\n\n");
  8.  
  9.     int a [ARY_SIZE];
  10.     int b [ARY_SIZE];
  11.  
  12.     printf ("Enter 10 numbers for array A.\n");
  13.     for (int i = 0; i < ARY_SIZE; i++)
  14.         scanf("%d", &a [i]);
  15.  
  16.  
  17.     printf ("\nEnter 10 numbers for array B.\n");
  18.     for (int i = 0; i < ARY_SIZE; i++)
  19.         scanf("%d", &b [i]);
  20.  
  21.  
  22.     printf ("\n\nArray A: ");
  23.     for(int i = 0; i < ARY_SIZE; i++)
  24.         printf("%d ", a [i]);
  25.     printf("\n");
  26.  
  27.     printf ("\nArray B: ");
  28.     for(int i = 0; i < ARY_SIZE; i++)
  29.         printf("%d ", b [i]);
  30.     printf("\n");
  31.  
  32.  
  33.     printf ("\nThe lines match: \n");
  34.     //this is where I need a loop to return "true" or "false"
  35.  
  36.  
  37.  
  38.  
  39.     return 0;
  40. }
  41.  
Apr 7 '10 #4
bryon
12
!!!!!!!!!!!!!!!!!! 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:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #define ARY_SIZE 10
  4.  
  5. int main(void)
  6. {
  7.     printf ("Welcome please enjoy this program.\n\n");
  8.  
  9.     int a [ARY_SIZE];
  10.     int b [ARY_SIZE];
  11.  
  12.     printf ("Enter 10 numbers for array A.\n");
  13.     for (int i = 0; i < ARY_SIZE; i++)
  14.         scanf("%d", &a [i]);
  15.  
  16.  
  17.     printf ("\nEnter 10 numbers for array B.\n");
  18.     for (int i = 0; i < ARY_SIZE; i++)
  19.         scanf("%d", &b [i]);
  20.  
  21.  
  22.     printf ("\n\nArray A: ");
  23.     for(int i = 0; i < ARY_SIZE; i++)
  24.         printf("%d ", a [i]);
  25.     printf("\n");
  26.  
  27.     printf ("\nArray B: ");
  28.     for(int i = 0; i < ARY_SIZE; i++)
  29.         printf("%d ", b [i]);
  30.     printf("\n");
  31.  
  32.  
  33.     printf ("\nThe arrays match:");
  34.     for (int i = 0; i < 1; i++)
  35.     {
  36.         if (a[0] != b[0])
  37.             printf("False\n\n");        
  38.         else if (a[1] != b[1])
  39.             printf("False\n\n");
  40.         else if (a[2] != b[2])
  41.             printf("False\n\n");
  42.         else if (a[3] != b[3])
  43.             printf("False\n\n");
  44.         else if (a[4] != b[4])
  45.             printf("False\n\n");
  46.         else if (a[5] != b[5])
  47.             printf("False\n\n");
  48.         else if (a[6] != b[6])
  49.             printf("False\n\n");
  50.         else if (a[7] != b[7])
  51.             printf("False\n\n");
  52.         else if (a[8] != b[8])
  53.             printf("False\n\n");
  54.         else if (a[9] != b[9])
  55.             printf("False\n\n");
  56.     else
  57.         printf("True\n\n");
  58.     }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.     return 0;
  65. }
  66.  
Apr 7 '10 #5
jkmyoung
2,057 Expert 2GB
Two methods for doing this more cleanly, the flag method and the break method.

Flag:
Expand|Select|Wrap|Line Numbers
  1. bool match = true; // assume they match beforehand
  2.    for (int i = 0; i < 10; i++) 
  3.     { 
  4.         if (a[i] != b[i]) 
  5.         {
  6.             match = false;
  7.         }
  8.     }
  9. printf(match); 
break method. (you'd probably lose marks if you submitted this though).
Expand|Select|Wrap|Line Numbers
  1.    for (int i = 0; i < 10; i++) 
  2.     { 
  3.         if (a[i] != b[i]) 
  4.              break;
  5.     }
  6.    if (i<10) printf("false");  // i<10 only if we used break to exit the loop.
  7.    else print("true");
Apr 7 '10 #6
bryon
12
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.
Apr 8 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.