Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,374 C++ Programmers. There are 1,418 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

C++

 
Reply to this topicStart new topic

C++

Vimrod
post 7 May, 2008 - 10:50 PM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 18



CODE

#include <iostream>

using namespace std;

void read(int a[], int n);
// read the first n elements of array a

bool sameValues(int a[], int b[], int size);
// compare two arrays of the same size
// It returns true if a and b contain the same values

const int SIZE = 5;

int main()
{
    
    int list1[SIZE], list2[SIZE];  
    
    read(list1, SIZE);
    read(list2, SIZE);
    
    if (sameValues(list1, list2, SIZE))
      cout << "The two arrays contain exactly the same values." << endl;
    else
      cout << "The two arrays contain different values." << endl;

    cout << endl;
    system("pause");
    return 0;
}

void read(int a[], int n)
{
     int counter;
     int list1[SIZE];

     cout << "Enter 5 values:" << endl;
          for (counter = 0; counter < 5; counter++)
          {
              cin >> list1[SIZE];
          }
}

bool sameValues(int a[], int b[], int size)
{
     int list1[SIZE], list2[SIZE];
    
     if (list1[SIZE]=list2[SIZE])
     return true;
     else return false;
}

My program is suppose to do that following:
1. write a C++ function void read(int a[], int n) to read the first n values for array a

2. write a C++ function bool sameValues(int a[], int b[], int size) that returns true if arrays a and b of the same size contain exactly the same values, otherwise false.

My output is:
Enter 5 values:
1 2 3 4 5
Enter 5 values:
1 2 3 4 5
The two arrays contain exactly the same values.

Press any key to continue . . .

But if it has different numbers it outputs:
Enter 5 values:
1 1 1 1 2
Enter 5 values:
2 1 1 1 2
The two arrays contain exactly the same values.

Press any key to continue . . .

But should be:
Enter 5 values:
1 1 1 1 2
Enter 5 values:
2 1 1 1 2
The two arrays contain different values.

Press any key to continue . . .

It just outputs that "The two arrays contain exactly the same values." even though the five integers are all different. What's wrong? thx in advance!




User is offlineProfile CardPM
Go to the top of the page
+Quote Post


no2pencil
post 7 May, 2008 - 10:54 PM
Post #2


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,322

QUOTE
cpp
if (list1[SIZE]=list2[SIZE])

This statement will always return true, because list1[size] can be set to the value of list[size].

Instead try :
cpp
if (list1[SIZE]==list2[SIZE])


This will compare if list1[size] is equal to list2[size].
User is online!Profile CardPM
Go to the top of the page
+Quote Post

skater_00
post 7 May, 2008 - 11:09 PM
Post #3


D.I.C Head

**
Joined: 30 Apr, 2008
Posts: 50

Yes, == is the operator to check if two variables of any data type are equal or not. But, to put the content of one variable into another variable, you use the = operator.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Vimrod
post 7 May, 2008 - 11:29 PM
Post #4


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 18

QUOTE(no2pencil @ 7 May, 2008 - 10:54 PM) *

QUOTE
cpp
if (list1[SIZE]=list2[SIZE])

This statement will always return true, because list1[size] can be set to the value of list[size].

Instead try :
cpp
if (list1[SIZE]==list2[SIZE])


This will compare if list1[size] is equal to list2[size].

Yeah i tried that before too, it then says that it does not match.

Enter 5 values:
1 2 3 4 5
Enter 5 values:
1 2 3 4 5
The two arrays contain different values.

Press any key to continue . . .

But thanx for ur help!
hm..still not sure why its doing that.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

no2pencil
post 7 May, 2008 - 11:33 PM
Post #5


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,322

Well, lets have a look at your function

QUOTE

cpp

bool sameValues(int a[], int b[], int size)
{
int list1[SIZE], list2[SIZE];

if (list1[SIZE]=list2[SIZE])
return true;
else return false;
}


You are declaring two arrays, list1 & list2, upon entry of the function. Then, without giving them any values, you compare the two. Of course they won't be equal. I see that you are passing in two arrays, a & b, however you don't even use these values.
User is online!Profile CardPM
Go to the top of the page
+Quote Post

no2pencil
post 7 May, 2008 - 11:45 PM
Post #6


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,322

This is how I would check the array. It's a simple program, minus all of the input stuff.

cpp

#include <iostream>

bool sameValues(int a[], int b[], int size);

int main(void) {
int i=0, a[6], b[6], size=5;

for(i;i<6;i++) {
a[i]=0;
b[i]=0;
}

/* Now our arrays have value, compare them */
if(sameValues(a, b, size))
printf("The two arrays contain exactly the same values.\n");
else printf("The two arrays contain different values.\n");

return 0;
}


bool sameValues(int a[], int b[], int size) {
int i=0;
for(i;i<=size;i++) {
if(a[i]!=b[i]) return false;
}
return true;
}
User is online!Profile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/17/08 02:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month