Why can't my function work the same way as a 1d array?

My question is why can't my program work the same way without 100 100

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
#include <iostream>
#include <string>
#include <cstring>

using namespace std;
//int number,number1,sum,sum1;
//int sun[][];


int prototype(int sun[100][100],int stone,int cold,int stain,int time,int masiv1[]) {
int sum;
for(stain=0;stain<stone;stain++)
{
sum=0;
for(time=0;time<cold;time++)
{
if(time<0)
{
sum+=0;
}
else
{
sum+=sun[stain][time];
}
}
masiv1[stain]=sum;
}

}

int main ()
{
int masiv[100][100],counter,counter1,number,number1,masiva[50];
cout<<"How many rows? " && cin>>counter;
cout<<"How many columns? " && cin>>counter1;

for(number=0;number<counter;number++)
{
for(number1=0;number1<counter1;number1++)
{
cout<<"Enter the "<<number1<<" column of the "<<number<<" row " && cin>>masiv[number][number1];
}
}
prototype(masiv,counter,counter1,number,number1,masiva);
for(number=0;number<counter;number++)
{
cout<<masiva[number]<<"\n";
}

}

I don't want the two hundreds in the function
I mean it works on a 1d array why can't it work on a multidimensional one?
In order to work out the location of an array element in memory using an index the size of the elements must be known.

If you have an array of int the memory location can be calculated as addressOfArray + sizeOfInt * index.

A multidimensional array is an array of arrays. masiv is an array of 100 arrays of 100 integers. As I said above, the size of the elements must be known. In this case the elements are arrays so that is why you need to specify the size of the arrays.

Note that you don't need to specify the first dimension.

 
int sun[][100]
Wow, that code was pretty obscure. Meaningful variable names go a long way to making readable code. A function named prototype and a variable named stain? I suspect you might be teasing us.

But in case you're not, here's how to do it with just one array:
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
#include <iostream>

using namespace std;

int main ()
{
    int totals[50] = { 0 };
    int numrows;
    int numcols;
    int number;
    
    cout << "How many rows? ";    cin >> numrows;
    cout << "How many columns? "; cin >> numcols;

    for (int row=0; row<numrows; row++)
    {
        for (int col=0; col<numcols; col++)
        {
            cout << "Enter the " << col+1 << " column of the " << row+1 << " row ";   
            cin >> number;
            totals[row] += number;
        }
    }
    
    for (int row=0; row<numrows; row++)
    {
        cout << totals[row] << "\n";
    }

}

I know the names aren't anything special.
But when i'm doing certain tasks hat need to be done in a specific way and I know I can do them without problem I name the variables and functions to whatever comes to mind.
The problem here was to know why it happend and how to make it work without puting the array size with little to no modifications.
I will try all of the above when I get back from work.
Thank you for your time and answers
chervil your way is really good.
But can you exlpain to me why does it have zero in totals[50]={0}?
Other than that you're using a 1d array to store all the sum for each row of columns.
by simulating a 2d array.
Which I have never thought of.
But I am required to use a function.
why does it have zero in totals[50]={0}?

The ={0} assigns initial values to the entire array. The first element, totals[0] is directly assigned the zero. The remainder are given a default value which is also zero. With a recent compiler in C++11 or later mode, the syntax can be simplified to totals[50] {} which uses default value for all the elements.

But I am required to use a function.

My apologies, I think I misunderstood what the original question was asking.

I think you could use a function with a header like this:
 
void calculate_row_totals (int grid[][100], int numrows, int numcols, int totals[])

There is no need to pass any additional parameters, the other variables can be defined locally inside the body of the function.
Still thank you and it's ok.
can you recommend any other ways for the function without using the 100?.If not I understand I'll move onto pointers.
Topic archived. No new replies allowed.