How Can I Finish Creating and Utilizing Arrays Program?

I am having issues plugging in values, variables and arrays in their correct places in this program. I am also having issues with creating the functions and the function prototypes needed for the program.

I am trying to code this program to do several things: (1) To get the total of all values stored in an array. (2) To get the average of all of the array values. (3) To get the total of all of the arrays values. (4) To get the total of all of the columns values. (5) To get the highest number in the row. (6) To get the lowest value in the array.

The array is 5 columns with 4 rows:

Row #1: 4, 24, 62, 91, 101
Row #2: 77, 0, 45, 33, 6
Row #3: 56, 7, 1, 27, 211
Row #4: 1, 4, 0, 22, 50


Here is what I have coded so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

// Program to create and utilize arrays to store lists of related data

#include <iostream>

using namespace std;

// Created arrays
int getTotal[4][5] = ;
double getAverage[4][5];
int getRowTotal[4];
int getColumnTotal
int getHighestInRow
int getLowestInRow

int main()
{
    getTotal();

    getAverage();

    getRowTotal();

    getColumnTotal();

    getHighestInRow();

    getLowestInRow();
}

// Created user defined functions

// Created function to sum  and display the total of all of the array elements added together
int getTotal() {

for (int row = 0; row < NUM_ROWS; row++){

    for (int col = 0; col < NUM_COLS; col++)
    total += numbers[row][col];
}
// Display the sum of the array
cout << "The total is " << total << endl;
}

// Created function to return the average of all of the array values
double getAverage(){

    for (int row = 0; row < ; row++)
    total += [row][col];
    average = total / NUM_SCORES;
    cout << "The average for all of the array values "
    << (row + 1) << " is " << average <<endl;
}

// Created function to get the total of the rows
int getRowTotal() {

    for (int row = 0; row < ; row++) {
    total = 0;
}
}

// Created function to get the total of the columns
int getColTotal() {

    for (int row = 0; row < ; row++)
    total += scores[row][col];
}


// Created function to get the highest number in the rows
int getHighestInRow() {

}

// Created function to get the lowest number in the rows
int get LowestNumber() {

}
First thing to do with this code is fix all of the compile errors, there are quite a few of them http://cpp.sh/2yvt7

C++ compilers can provide confusing messages, so take them one at a time, start with the first one, fix it and then recompile.

Don't try to plough through the error list because some of them are only there because of a previous error. Just do the first one, then recompile to get a fresh error list. then repeat.

If you have any problems with that, post back here :)

I am going to get you started a bit.
you need to pass the array to the function.
total is easy; you can 'collapse' it to one dimension.

1
2
3
4
5
6
7
8
9
10
11
12
int total(int* ip, int size)
{
     int i,result = 0;
     for(i = 0; i < size; i++)
        result += ip[i];
return result;     
}

main...
int getTotal[4][5] = {{1,2,3,4,5},{6,7,8,9,10},... add more};
cout << total (getTotal, 4*5) << endl;  


if you need 2d functionality, its a little more tricky, you need to know the dimensions partly up front. pseudocode looks like
1
2
3
4
5
6
7
int getHighestInRow(int input[4][], int whichrow, int numcols) 
{
     ...  
       for(i ... all the columns)
        if( biggest)
       result = input[whichrow][i]
}

(1) To get the total of all values stored in an array.
(2) To get the average of all of the array values.
(3) To get the total of all of the arrays values.
(4) To get the total of all of the columns values.
(5) To get the highest number in the row.
(6) To get the lowest value in the array.

It is hard to say, how 1 and 3 differ. 6 is suspicious too.

How about:
1. Sum of all elements of 2D array.
2. Average of all elements of 2D array. (==sum/count)
3. Sum of elements of one row. (repeat for each row)
4. Sum of elements of one column. (repeat for each column)
(sum of all == sum of row-sums == sum of column-sums)
5. Maximum element of one row. (repeat for each row)
6. Minimum element of one row. (repeat for each row)


Your lines 9-14 (attempt to) declare variables. They are global variables; in "file scope".
You could do so, but you will (have to) learn more, if you do use local variables.
A function is much more usable, when it does not depend on globals, but uses parameters and return value.

A function that calculates should (preferably) not print anything.

First a "scary" example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::vector<int>> data
    { {4, 24, 62, 91, 101},
      {77, 0, 45, 33, 6},
      {56, 7, 1, 27, 211},
      {1, 4, 0, 22, 50} };

    for ( const auto & row : data ) {
        auto hilo = std::minmax_element( row.begin(), row.end() );
        std::cout << "row max " << *hilo.second << " min " << *hilo.first << '\n';
    }
}

But, but, We haven't had vectors yet!

No problem. Plain array is just as fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>

int main()
{
    constexpr size_t Cols {5};
    int data[][Cols]
    { {4, 24, 62, 91, 101},
      {77, 0, 45, 33, 6},
      {56, 7, 1, 27, 211},
      {1, 4, 0, 22, 50} };

    for ( const auto & row : data ) {
        auto hilo = std::minmax_element( std::begin(row), std::end(row) );
        std::cout << "row max " << *hilo.second << " min " << *hilo.first << '\n';
    }
}

What an odd loop you have and can we do just max?

How quaint, but why not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>

int main()
{
    constexpr size_t Cols {5};
    constexpr size_t Rows {4}; // cheat, we should read this from the array
    int data[][Cols]
    { {4, 24, 62, 91, 101},
      {77, 0, 45, 33, 6},
      {56, 7, 1, 27, 211},
      {1, 4, 0, 22, 50} };

    for ( size_t row=0; row < Rows; ++row ) {
        auto hi = std::max_element( data[row], data[row] + Cols );
        std::cout << "row " << row << " max " << *hi << '\n';
    }
}

Our function is still a one-liner from Standard Library. What magic is that?

I presume that you have (not) seen:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/
You should (re)read them. There is section "Arrays as parameters". Short, not sweet.

The data[row] and data[row] + Cols are pointers to first element and one-past last element of one row of the 2D array.

An alternative is to give a pointer to first element and the count of elements.

We could make the function more complex by giving pointer to first element of the 2D array, the index of row that we are interested in, and the count of elements in one row.

We could make the function yet more complex by giving pointer to first element of the 2D array, the count of rows, the count of elements in one row, and pointer to 1D array (which must have at least rows elements).

Fun!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    constexpr size_t Cols {5};
    constexpr size_t Rows {4};
    int data[][Cols]
    { {4, 24, 62, 91, 101},
      {77, 0, 45, 33, 6},
      {56, 7, 1, 27, 211},
      {1, 4, 0, 22, 50} };

    int rowmax[Rows];

    for ( size_t row=0; row < Rows; ++row ) {
        auto max1 = foo( data[row], Cols );
        auto max2 = bar( data, Cols, row );
        // show max1 or max2
    }

    gaz( data, Rows, Cols, rowmax );
    for ( size_t row=0; row < Rows; ++row ) {
        // show rowmax[row]
    }
}

Oh, I forgot to declare and implement the functions foo, bar, and gaz.

What parameter types do they need? When used as parameter
data[row] has type int*
data has type int**
Cols has type size_t
Rows has type size_t
row has type size_t
rowmax has type int*

The foo and bar do return an int value.
The gaz does not return a value and should have return type void. It stores results into its fourth parameter.
That I could write:
void gaz( const int** matrix, size_t rows, size_t cols, int* rmax );
What you all understand and how you all use code is very impressive.

Can you please speak laymanese? Lay person concepts? :-)
Last edited on
Did you read the two tutorials that I linked to?
did you try to fix the compiler errors that your code generates?

http://cpp.sh/3yq3
Can you please speak laymanese? Lay person concepts?

If there are specific things people have said that you don't understand, then I'd recommend asking specific questions about those things. That way, we can focus on explaining what needs to be explained.

Just asking people to rewrite their posts in the hope that they stumble across what you're looking for isn't helpful.
if you didn't understand you may want to write a small program using single dimensional arrays and get a good grasp of that. starting with 2-d is making it more difficult to learn and understand.
Topic archived. No new replies allowed.