Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 15th, 2004, 3:07 PM   #1
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
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!
sys0p is offline   Reply With Quote
Old Oct 15th, 2004, 3:25 PM   #2
kilgayne
Newbie
 
kilgayne's Avatar
 
Join Date: Oct 2004
Location: Quebec
Posts: 10
Rep Power: 0 kilgayne is on a distinguished road
Send a message via MSN to kilgayne
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 : &quot;If you can't make it good, make it looks good.&quot;
kilgayne is offline   Reply With Quote
Old Oct 15th, 2004, 11:00 PM   #3
Das Bruce
Newbie
 
Join Date: Oct 2004
Posts: 10
Rep Power: 0 Das Bruce is on a distinguished road
I'm not sure if these are errors but you might want to look into it.

for( i = 0; i < array1[5]; i++ )
array1[5] is actually the 6th element.

int i;
for( i = 0;
could be compressed into
for(int i=0;
Das Bruce is offline   Reply With Quote
Old Oct 15th, 2004, 11:18 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Actually, my compiler complains when I compress the for loop like that. Don't know why.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 16th, 2004, 12:46 AM   #5
Das Bruce
Newbie
 
Join Date: Oct 2004
Posts: 10
Rep Power: 0 Das Bruce is on a distinguished road
What are you using?

I use DevC++ which comes with gcc. DevC++ home
Das Bruce is offline   Reply With Quote
Old Oct 16th, 2004, 5:11 AM   #6
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
#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++ 4

Cheers!
sys0p is offline   Reply With Quote
Old Oct 16th, 2004, 5:14 AM   #7
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Quote:
array1[5] is actually the 6th element.
Hey, uh that's why you say that i is less than array[5] which is the 6th element. but it doesnt stop you adding more than 5 numbers!? I can't figure out why :/
sys0p is offline   Reply With Quote
Old Oct 16th, 2004, 6:34 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You want:

for (i = 0; i < 6; i++) ...
You want it to go through 6 times, not until it becomes higher than whatever's in array[5].
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 16th, 2004, 7:17 AM   #9
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
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.
mici is offline   Reply With Quote
Old Oct 16th, 2004, 11:07 AM   #10
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Hey,
Thanks for the replies!

Quote:

You want it to go through 6 times, not until it becomes higher than whatever's in array[5].
Well supposing you didn't know how big your array was? How would you tell the counter to exit when it hit the last element in the array? Suppose you had an array my_array[100] and you only type in 5 elements, and your counter is set to for( i = 0; i < 100; i++ ) then it would output weird results. Or am I being totally stupid here? 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:

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...
OK I think I see the problem.

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:

So actualy your program works fine ... it just checks if element 0 from both arrays are equal and displays result and then breaks...
Please correct me if I am wrong: How can this be when your iterator starts at 1. That means that it checks array[1] first and not array[0], if you get what I mean?

Quote:

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....
Sorry, I know nothing about memcmp because I haven't ever heard about it. It's far beyond the scope of my knowledge. I still have to reach that chapter in the book i'm reading, Practical C++ Programming, 1st Ed. Unfortunately. But thanks for pointing that out.

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!
sys0p is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC