Need some help with this

This is a discussion on Need some help with this within the C++ Programming forums, part of the General Programming Boards category; I have to write a file that has 4 columns and an unspecified number of rows. It has to have ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    Need some help with this

    I have to write a file that has 4 columns and an unspecified number of rows. It has to have 3 void functions, the first is a readgrades function with two parameters double grades [][4], and int n for the number of rows. This function prompts the user for three grades, and the grades are read into the first three columns using a for loop

    The second function is calcavg that has the same two parameters . This one calculates the average , and places the average in the fourth column.

    The last function is printarray, same two parameters. This one displays the contents of the 2 dimensional array a. This uses a for loop as well.

    Finally the main calls the functions readgrades, calcavg and printarray.

    Here is what I have so far, but i'm pretty lost on this. I'm getting weird answers and errors. If anyone can help me fix this i'd appreciate it. ps. she wants void functions for some reason.

    Code:
    #include <iostream.h>
    void readgrades (int grade [][4], int n){
    int i, j;
    for (i=0;i<=n;i++){
    for (j=0;j<3;j++){
    cout <<"please enter a grade:  ";
    cin >> grade [i][j];
    }
    }
    void calcavg (double a[][4], int n){
    int i, j, sum=0;
    double average;
    for (i=0; i<=4; i++){
    for (j=0; j<=3;j++){
    sum=sum + a[i][j];
    }
    average = (double)sum/3;
    average=a[i][4];
    }
    }
    void printarray (double a[][4], int n)
    int i, j;
    for (i=0;i<=n;i++){
    for (j=0:j<=4;j++){
    cout <<a[i][j];
    }
    }
    void main (){
    int n;
    cout <<"Enter number of students:  ";
    cin >>n;
    int a= new int [n][4];
    readgrades (a,n);
    calcavg(a,n);
    printarray(a,n);
    cout<<endl;
    }

  2. #2
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21

    Re: Need some help with this

    To start debugging your code you have to first do a lot yourself. There are a lot of brakets that are missing. There are improper syntax in some of the for loops.

    You are probably getting a lot of errors for you array and that is because C++ doesn't like variable length arrays so you'll have to look into using CArray (a dynamic memory based array system).

    After you fix those problems then you're program should have less errors in it and easier ones to figure out.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21