2d array

Hi ! I'm new to programing and I'm learning on my own so it's quite hard for me to do tasks without any examples . Can someone help me with a task ? There's an array stud(m,n) in which m is students and n is grades . create a new array avg(m) which is made from students grade average . It is told to use a 2d array. This is some code i wrote but it only shows what did the user input.

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
  #include <iostream>
using namespace std;

int main(void)
{
    
    int m,n ;
    int myArray[10][10]={n,m};
   
    cout<<"how many students is there ? ";
    cin>>n;
    cout<<"how many classes does he have ? ";
    cin>>m;
  //   myArray[width][height];
    
    for (int i = 0; i < n; ++i)
    { cout<<i+1<<"-ist student "<<"\n";
        for (int j = 0; j < m; ++j)
        {
            cout<<(j+1)+n*i<<": grade ";
            cin>>myArray[i][j];
        }
      
    }


int masyvas [10][10];

for (int i=0;i<n;i++){
    for (int j=0;j<m;j++){
        masyvas[i][j]=myArray[i][j];
    }
    
}
    

    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            cout<<masyvas[i][j]<<" ";
    
    
        }
  
        cout <<endl;
    }
  
}
Last edited on
closed account (48T7M4Gy)
Can someone help me with a task ?
Which task are you concerned about?
I just don't know how to make an array from students grade average.
Your first array is an array of up to 10 students with up to 10 test grades. So now you want to compute the average test grade for each student and store these averages in a single dimensional array, one element for each student.

By the way on line 8 there is no need for the initialization, especially since both m and n are not initialized.

Also you should be checking that the user of your program enters 10 or less for each of these variables, since your array is set to 10.

wrote this code :
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
#include <iostream>
using namespace std;

int main(void)
{
    
    int m,n,a,b,r,c ;
    double total;
    int myArray[10][10]={n,m};
    int vidurkis[10];
    double average =0.00;
    a=0;
   
    cout<<"how many students is there ? ";
    cin>>n;
    cout<<"how many classes does he have ? ";
    cin>>m;
  
    
    for (int i = 0; i < n; ++i)
    { cout<<i+1<<" student "<<"\n";
        for (int j = 0; j < m; ++j)
        {
            cout<<(j+1)+n*i<<": grade ";
            cin>>myArray[i][j];

            for (int x=0;x<j;x++){
        
            }
        }
      
    }

    for (int r=0;r<n;r++){
        cout<<"student "<<r+1<<" ";
    
    for (int c=0;c<m;c++){
        cout<<myArray[r][c]<<" ";
        total+=myArray[r][c];
    }
    
    average=total/m;
    cout<<"Average: "<<average<<endl;
    total=0;
    average=0.00;
    }
    return 0;
    

    
   
}

everything works !!!
Yes it "works", but does it meet the assignment requirements? Where are you using the vidurkis[] array? Where are you using variables b, c, and r?

I was experimenting with the program and forgot to delete unused variables.
Last edited on
Topic archived. No new replies allowed.