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,786 people online right now. Registration is fast and FREE... Join Now!




Maximum Value with Arrays

 
Reply to this topicStart new topic

Maximum Value with Arrays

chevygrl
11 Apr, 2007 - 09:43 AM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 1


My Contributions
i have been working on this for hours, and I can't figure out what im doing wrong. This is my first c++ class and i have struggled every step of the way. Im tired of trying this program for 5 hours straight. Help PLZ

the progrm finds and dispays maxium value in a two diminsional arry of integers. 4 by 5 array
then we have to modify it to show the subscript value
Here is what I have managed to come up with.
It doesn't show my arrays, the maximum value, and the maximum subscripts are wrong. Im pulling out my hair!
CODE

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

int main ()

{
const int firstrow = 4;
const int seccol = 5;

int data [firstrow][seccol] = {16, 22, 99, 4 ,18, -258, 4, 101, 5, 98, 105, 6, 15, 2, 45, 33, 88, 72, 16, 3};

int bigF = 0;
int bigS = 0;

int row, col;
for (row = 0; row < firstrow; row++)
{
for (col = 0; col < seccol; col++)
if (data[row][col] > data [bigF][bigS])
cout<< setw(4) << data[row][col];
}
{
bigF = row;
bigS = col;
}

cout << "   Maximum value Row Subscript value:      " << bigF  <<endl;
cout << "   Maximum value Column Subscript value:   " <<bigS << endl;
cout << setw(5) << data[firstrow] [seccol];

return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Maximum Value With Arrays
11 Apr, 2007 - 10:13 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
If you're looking for a maximum value, you simply need to loop through the array, and check each one:
CODE

int maxval=myarray[0][0];
int colindex=0
int rowindex=0;
for(int i=0;i<4;i++){
   for(int j=0;j<5;j++){
      if(myarray[i][j]>maxval){
         maxval=myarray[i][j];
         colindex=j;
         rowindex=i;
      }
   }
}

or something similar should suffice.

you'll also probably want to look at your array instantiation.
User is offlineProfile CardPM
+Quote Post

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

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