![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
Hi,
As I am learning C++ i've decided to post my code to help anyone out there. Of course most of you will be godlike programmers, but for us, the newbs this might help ![]() Below is some code to allow the user to input 5 integer numbers into two arrays, array1 and array2. If the numeber's are the same i.e array1 = { 1, 2, 3, 4, 5 }; array2 = { 1, 2, 3, 4, 5 }; Then the message "The array's are equal" is output, else the message "The array's are not equal" is output.
#include <iostream.h>
int array1[5];
int array2[5];
main()
{
cout << "This program determines if the number's you enter into array 1 and array2 are identical" << '\n';
cout << "Please enter 5 numbers into this array: ";
cin >> array1[0] >> array1[1] >> array1[2] >> array1[3] >> array1[4];
cout << "and do enter 5 numbers into this array: ";
cin >> array2[0] >> array2[1] >> array2[2] >> array2[3] >> array2[4];
int i;
for( i = 0; i < array1[5]; i++ )
{
if(array1[i] != array2[i]) {
cout << "The array's are not equal" << '\n';
break;
}
cout << "The array's are equal";
}
return(0);
}If there are any mistakes here please point them out. After all the purpose of this code is to help us learn ![]() Oh, maybe some suggestions as how to improve the program. One would be to say: if size of array1 is not equal to size of array2, then break. Obviously. But I haven't implemented it in the code yet. Cheers! ![]() |
|
|
|
|
|
#2 |
|
Newbie
|
iostream.h sucks
conio.h rulez \o/ cprintf("I guess this could eventually help n00bs.\r\nDon't give up!");
__________________
yeah, i suck ^^; Quote from Bill Gates : "If you can't make it good, make it looks good." |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 10
Rep Power: 0
![]() |
I'm not sure if these are errors but you might want to look into it.
for( i = 0; i < array1[5]; i++ ) int i; for( i = 0; for(int i=0; |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Actually, my compiler complains when I compress the for loop like that. Don't know why.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2004
Posts: 10
Rep Power: 0
![]() |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
#include <iostream.h>
int array1[5];
int array2[5];
main()
{
cout << "This program determines if the number's you enter into array 1 and array2 are identical" << '\n';
cout << "Please enter 5 numbers into this array: ";
cin >> array1[0] >> array1[1] >> array1[2] >> array1[3] >> array1[4];
cout << "and do enter 5 numbers into this array: ";
cin >> array2[0] >> array2[1] >> array2[2] >> array2[3] >> array2[4];
int i;
for( i = 0; i < array1[5]; i++ )
{
if(array1[i] != array2[i]) {
cout << "The array's are not equal" << '\n';
break;
}
cout << "The array's are equal";
break;
}
return(0);
}That should work. I swear the last bit worked for me, but I probably edited it by mistake. Sorry It should work now. I'm using Dev C++ 4Cheers! |
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You want:
for (i = 0; i < 6; i++) ... |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
Ooble is right...
you've been checking if your iterator is lower than the member no. 5 in your array... by default array[5] is not defined and can be any number until you set it... i.e it can be 4331 or 0 or -234 ... this is why your "for" loop won't work as it should... The probable reason why it ends after 5 loops is that since both array1 and array2 have not been defined and as such are probably bigger positive nums left from some process before the loop finds that they are not equal and breaks there... another thing is that your program shouldn't work at all... if you observe more closely you'll see that the "if" checks if the array1[i] is equal to array2[i] and then if they aren't equal function breaks from the for loop wich is ok, however if they are equal the function also breaks and it never checks for "i" larger than "0". So actualy your program works fine ... it just checks if element 0 from both arrays are equal and displays result and then breaks... Now for the solution you can eather try to repair your code ... remove the breaks and iclude counter for num of equals or !equals... The other solution, much faster and easier to progam is to use memcmp() like this memcmp(array1, array2, 5*2); 5*2 is because int takes 2 bytes so if you used chars it would be just 5 and if you used doubles it would be 5*4. memcmp will return 0 if both fields are equal.... Hope this helps a little. ![]()
__________________
coffee is my heroin. |
|
|
|
|
|
#10 | ||||
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
Hey,
Thanks for the replies! Quote:
Logic says that I am being stupid because the iterator goes through 99 elements, then to the last element \0 in the array. So it goes over each element, if the first 5 have been typed it goes through:array1[0] array1[1] ... array1[4] //Hits the last value actually entered into the array array1[5] array1[99] //Iterates through all the rest of the elements, which contain nil array1[100] //Hits the last element which is \0 So either way, if you enter 5 values or 99 it still iterates through the array. Right? Quote:
Actually I had a nasty feeling that it was a fluke I was about to post and say that how could it compare array1[i] and array2[i] if you didn't create a loop for array2. Or am I being stupid here? Quote:
Quote:
![]() TO CLEAR UP ON A FEW THINGS: #1 In the for loop: for( i = 0; i < array1[5]; i++ ) If we assign the increment variable to our array like so array[i] then the array is iterated over starting from element 1 and not element 0. Is that right? #2 When I created the for loop does the i < array[5] in for( i = 0; i < array1[5]; i++ ) refer to element 5 in the array, or simply refer to array1 which has 5 elements? From the replies above i'm assuming that it refers to the 6th element in array1. Right? #3 So if I was to create a proper version of this program, how exactly would I implement this? Would it be like so: #include <iostream.h>
int array1[5];
int array2[5];
main()
{
cout << "This program determines if the number's you enter into array 1 and array2 are identical" << '\n';
cout << "Please enter 5 numbers into this array: ";
cin >> array1[0] >> array1[1] >> array1[2] >> array1[3] >> array1[4];
cout << "and do enter 5 numbers into this array: ";
cin >> array2[0] >> array2[1] >> array2[2] >> array2[3] >> array2[4];
int i;
for( i = 0; i < 6; i++ )
{
if(array1[i] != array2[i]) {
cout << "The array's are not equal" << '\n';
}
cout << "The array's are equal";
}
return(0);
}I tried the above code and when the arrays are equal it prints the following: and do enter 5 numbers into this array: 1 2 3 4 5 The array's are equalThe array's are equalThe array's are equalThe array's are equalThe array's are equalThe array's are not equal And if the arrays are not equal it prints the following: Please enter 5 numbers into this array: 1 2 3 4 5 and do enter 5 numbers into this array: 5 4 3 2 1 The array's are not equal The reason I added the break statement after each cout was to stop the cout repeating itself. Lol, i'm already getting bad headaches thinking about this! ![]() I just want to thank all of those who have helped me. I really appreciate your help. And, I want to apologise to all those for posting this corrupt and buggy code. After all I am a newb Cheers! |
||||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|