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

Join 136,124 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,773 people online right now. Registration is fast and FREE... Join Now!




Comparison of array elements

 
Reply to this topicStart new topic

Comparison of array elements

ZORO
8 Apr, 2007 - 10:25 AM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 6


My Contributions
Is this any logical errors in this code? why I can't find the result? Plz help me, thank you.

CODE
   int max = 0;
            for ( int i =1; i < arraySize; i++)
            {
                   if ( Sales[i] >= Sales[max])
                   max = i;
             }
     cout << Sales[max] << endl;



User is offlineProfile CardPM
+Quote Post

BitByte
RE: Comparison Of Array Elements
8 Apr, 2007 - 11:56 AM
Post #2

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
Arrays start at zero

EDIT: What is the output you are getting? What are you expecting to get? The loop is fine. Apart from not starting at zero.

Try this way

CODE
int max = Sales[ 0 ];

for ( int i =0; i < arraySize; i++ )
{
    if ( Sales[i] >= max )
        max = i;
}

cout << Sales[max] << endl;


This post has been edited by BitByte: 8 Apr, 2007 - 12:10 PM
User is offlineProfile CardPM
+Quote Post

mattman059
RE: Comparison Of Array Elements
8 Apr, 2007 - 06:28 PM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
There are reasons why you would want your index to not be 0... you could just refer to it in the array as

CODE


myArray[index-1];


User is offlineProfile CardPM
+Quote Post

ZORO
RE: Comparison Of Array Elements
8 Apr, 2007 - 09:33 PM
Post #4

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 6


My Contributions
I still can not find the answer. Acturally, it is a two-dimensional array, and i just want to compare the last row of the total.
The whole code is below:

CODE

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
     const product = 6;
     const people = 5;
    
     double Sales[product][people]={0};
    
     double sum1,sum2;
     for (int i=0; i<people-1; i++)
     {
         cout<< "Salepeople " << i+1 << endl;
             for(int j=0; j<product-1; j++)
             {
                 cout<< "Product " << j+1 <<" : ";
                 cin>> Sales[j][i];
             }
         cout<<"------------------------------"<<endl;
     }
    
     cout << "The Sales Report of the Company." << endl;
     cout <<"************************************************************************" << endl;
     cout << setw(19)<< "Product 1" <<setw(11)<< "Product 2" <<setw(11)<< "Product 3" <<setw(11)
          <<"Product 4"<< setw(11)<< "Product 5"<< setw(9)<< "Total" << endl;
     for (int k=0; k<people-1; k++)
     {
         cout<<"People "<< k+1;
         sum1 = 0;
         for (int h=0; h<product-1; h++)
         {
             cout<< setw(11) << Sales[h][k];
             sum1 += Sales[h][k];
         }
         cout << setw(9) << sum1;
         cout << endl;
     }
    
     cout << "Total   ";
     for (int x=0; x<product-1; x++)
     {
         sum2 =0;
         for (int y=0; y<people-1; y++)
         {
             sum2 += Sales[x][y];
         }
         cout << setw(11) << sum2;
     }
     cout << endl;
     cout << "************************************************************************" << endl;
           
     int max = Sales[0][4];

     for ( int index =0; index < product-1; index++)
     {
         if ( Sales[index][5] >= max)
         max = index;
     }
     cout << Sales[max][5] << endl;
     return 0;
}




User is offlineProfile CardPM
+Quote Post

ZORO
RE: Comparison Of Array Elements
9 Apr, 2007 - 04:54 AM
Post #5

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 6


My Contributions
for help

User is offlineProfile CardPM
+Quote Post

mattman059
RE: Comparison Of Array Elements
10 Apr, 2007 - 03:07 PM
Post #6

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
What compiler are you using?

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Comparison Of Array Elements
10 Apr, 2007 - 09:52 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Ok there is a logic error... you are setting max to the "index of the maximum element" but you are compairing that to an element... get it, you are compairing a value, with the index to a value...

CODE
     int max = Sales[0][4];

     for ( int index =0; index < product-1; index++)
     {
         if ( Sales[index][5] >= max)
         max = Sales[index][5];
     }
     cout << max << endl;


Your original code did not have this error. In fact the original post works just fine for me. In your code it would look like this:
CODE
     int max = 0;

     for ( int index =0; index < product-1; index++)
     {
         if ( Sales[index][5] >= Sales[max][5])
         max = index;
     }
     cout << Sales[max][5] << endl;

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:13PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month