473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Find highest and lowest value in a multidimensional array in C?

I can't get it to work to find the highest value in the given matrix. Any ideas? (total C beginner here).

Also in this one i get the user to fill in the array values but which would be the fastest way to have a predefined 2D multiarray?






#include <cstdlib>
#include <iostream>

using namespace std;

const int NODES = 3;
const int CONNECT = 7;
double average = 0;

int getSum (int [][CONNECT]);
void getChart (int [][CONNECT]);
void connectionAvg (int table [][CONNECT]);
int findLowest (int table [][CONNECT]);
int findHighest (int table [][CONNECT]);

int main(int argc, char *argv[])
{
int table [NODES][CONNECT];

cout << "The nodes and connections each, please enter your info tutti frutti: \n\n";

getChart(table);

cout << "The total amount of connections is: "
<< getSum(table) << " Connections\n\n";

connectionAvg(table);

cout << "The lowest amount of connections is : " << findLowest(table) << " Connections\n";
cout << "The highest amount of connections is: " << findHighest(table) << " Connections\n";

//ofstream outData;
//outData.open("results.txt");

system("PAUSE");
return EXIT_SUCCESS;
}

int getSum (int table [][CONNECT])
{
int sum = 0;
for ( int node = 0; node < NODES; node++)
{
for (int connection = 0; connection < CONNECT; connection++)
sum += table [node][connection];
}
return sum;
}

void getChart (int table[][CONNECT])
{
for (int node = 0; node < NODES; node++)
{
for (int connection = 0; connection < CONNECT; connection++)
{
cout << "node " << (node+1) << ", ";
cout << "connection " << (connection+1) << ": ";
cin >> table [node][connection];
}
cout << endl;
}
}

void connectionAvg (int table [][CONNECT])
{
for ( int connection = 0; connection < CONNECT; connection++)
{
int total = 0;
for (int node = 0; node < NODES; node++)
{
total += table [node][connection];
}
average = total/NODES;
cout << "Average connections on connection " << (connection+1)
<< " by all 3 NODES is: " << average << " connections" << endl;
}
}

int findLowest (int table [][CONNECT])
{
int lowest = table [NODES][CONNECT];
for (int node = 0; node < NODES; node++)
{
for (int connection = 0; connection < CONNECT; connection++)
{
if (table [node][connection] < lowest)
lowest = table[node][connection];
}

cout << endl;
}
return lowest;
}

int findHighest (int table [][CONNECT])
{
int highest = table [NODES][CONNECT];
for (int node = 0; node < NODES; node++)
{
for (int connection = 0; connection < CONNECT; connection++)
{
if (table [node][connection] > highest)
highest = table[node][connection];
}

cout << endl;
}
return highest;
}
Aug 27 '15 #1
1 1482
weaknessforcats
9,208 Expert Mod 8TB
I'm not sure why you are using a multidimensional array.

You might read this: http://bytes.com/topic/c/insights/77...rrays-revealed

This code:

Expand|Select|Wrap|Line Numbers
  1. int findHighest (int table [][CONNECT])
  2.  {
  3.  int highest = table [NODES][CONNECT];
  4. etc...
assigns highest from table[3][7]. However, the last element in the array is
table[2][6]. The same problem is in the lowest function as well. Remember, array indexes count from zero.
Aug 27 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
3
by: lostncland | last post by:
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense? /*Defines a global constant called N throughout the file. ...
30
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
9
by: raamay | last post by:
i have a table where the records are in the following fashion: firm w1 w3 w4 class abc ltd. b c d b bcd ltd. ...
2
Brandenburg
by: Brandenburg | last post by:
I'm writing a game in visual basic 2008 and things are going fairly well with it for the most part.. well until today.. I can declare a 1 dimensional array of a class just fine but cannot for the...
7
by: Alberto Fortuny | last post by:
Hey guys, any idea as to how to find the lowest value and the highest value in this 2D array? I made the functions to find the lowest and highest, but all im getting on execution is a 1 on both, no...
2
by: antonydoyle | last post by:
Hi Hope someone can help me with this! I'm trying to get a table of data into a multidimensional array. The table fields are as follows; headline
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.