Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

trouble converting 1-D array into 2-D

3a4zgz3a4zgz Posts: 7Member
I have a program for finding the grades in a class of 10 students that uses 1-D arrays. My code is posted below. How do I convert it into 2-D array? Below the code, I gave an example of how I tried to do it, but I don't think I'm going about it right. Please help!

[code]
#include
#include
#include
#include

using namespace std;

float calculateExams (float[]);
float calculateFinal (float);
float calculateProjects (float[]);
float calculateQuizzes (float[]);
void letterGrade (float[], int&, int&, int&, int&, int&, char&);

int main ()
{
ifstream inFile;
ofstream outFile;
inFile.open ("myInput.txt");
outFile.open ("result.txt");

int A = 0, B = 0, C = 0, D = 0, F = 0, x = 0, t;
char letter;
string first, last;
float exams[3], final, projects[8], quizzes[5], average[5];

while (x < 10)
{
t = 0;
inFile >> first >> last;

for(int m = 0; m < 3; m++)
{
inFile >> exams[m];
if (exams[m] < 0)
{
t++;
}
}

inFile >> final;
if (final < 0)
{
t++;
}

for(int m = 0; m < 8; m++)
{
inFile >> projects[m];
if (projects[m] < 0)
{
t++;
}
}

for (int m = 0; m < 5; m++)
{
inFile >> quizzes[m];
if (quizzes[m] < 0)
{
t++;
}
}

if (t > 0)
{
outFile << "Incorrect data for " << first << ' ' << last << endl << endl;
x++;
continue;
}

average[0] = calculateExams(exams);
average[1] = calculateFinal (final);
average[2] = calculateProjects (projects);
average[3] = calculateQuizzes (quizzes);
letterGrade (average, A, B, C, D, F, letter);

outFile << fixed << showpoint << setprecision(2)
<< first << ' ' << last << " has a(n) " << letter << " with a "
<< average[4] << "% for this course. His/Her grades are: " << endl
<< "Percentage of exams: " << average[0] << "%" << endl
<< "Percentage of final: " << average[1] << "%" << endl
<< "Percentage of projects: " << average[2] << "%" << endl
<< "Percentage of quizzes: " << average[3] << "%" << endl << endl;
x++;
}

outFile << "Total A's: " << A << endl
<< "Total B's: " << B << endl
<< "Total C's: " << C << endl
<< "Total D's: " << D << endl
<< "Total F's: " << F << endl;

return 0;
}

float calculateExams (float ex[])
{
float sum = 0;
for (int k = 0; k < 3; k++)
sum += ex[k];
return (sum/300) * 30;

}

float calculateFinal (float fin)
{
return (fin/100)* 30;
}

float calculateProjects (float proj[])
{
float sum = 0;
for (int k = 0; k < 8; k++)
sum += proj[k];
return (sum/400) * 30;
}

float calculateQuizzes (float q[])
{
float sum = 0;
for (int k = 0; k < 5; k++)
sum += q[k];
return (sum/100) * 10;
}

void letterGrade (float scr[], int& A, int& B, int& C, int& D, int& F, char& l)
{
scr[4] = scr[0] + scr[1] + scr[2] + scr[3];

int factor = int(scr[4]/10);

switch (factor)
{
case 10: case 9: {l = 'A';
A++;
}
break;
case 8: {l = 'B';
B++;
}
break;
case 7: {l = 'C';
C++;
}
break;
case 6: {l = 'D';
D++;
}
break;
default: {l = 'F';
F++;
}
}
}
[/code]

For each function, I changed the for loop to a nested for loop. For example, for the function calculateQuizzes, I changed it to:
[code]
float calculateQuizzes (float q[][])
{
float sum = 0;
for (int r = 0; r < 10; r++)
for (int c = 0, c < 5; c++)
sum += q[r][c];
return (sum/100) * 10;
}
[/code]
...and then up in the function declarations above the main, I changed it to "float calculateQuizzes (float[][]);". But there's got to be more to it than that! What else do I need to do?

Comments

  • Malcolm_McLeanMalcolm_McLean Posts: 248Member ✭✭
    : I have a program for finding the grades in a class of 10 students
    : that uses 1-D arrays. My code is posted below. How do I convert it
    : into 2-D array?
    :
    Be aware that 2D arrays are rather an advanced feature of C. Most books introduce them along with 1D arrays, without mentioning the special problems they cause.

    The central dicciulty is that a C array is just a buffer in memory. But when the array is 2D, the extra duimension information isn't stored anywhere. So you need to hardcode it in.

    Things like

    int foo2D(float parameter[][])

    are illegal because foo2D has no way of knowing how many entries are in each row. However

    int foo2D(float parameter[][3])

    is OK.

    The problem is that often your array dimensions aren't known at compile time. So 2D arrays can't be used at all, you have to fake them up from arrays of pointers, or simply treat as a 1D array and do the indexing calculations manually.
  • LundinLundin Posts: 3,711Member
    All of that is true. Before you start using multi-arrays, I'd advise you to make sure you know exactly how parameter passing to functions work, and exactly how dynamic memory allocation works.

    Next thing you must be aware of is that multi-arrays in C is [italic]an abstract programming structure[/italic] and not a mathematical vector/matrix. Unless of course, you specify it that way when you create them.

    To make things even more complicated and confusing, C supports many many ways to implement 2-d arrays. They all come with various advantages and disadvantages. You need to pick the one that suits your needs best.

    Static multi-array:

    [code]int array[x][y];[/code]

    Advantages: Fast, safe, compatible with ISO functions like memcpy().
    Disadvantages: Dimensions can't be set or changed in runtime. Possibly eats a lot of static memory.

    Static mangled multi-array:

    [code]int array[x*y];[/code]

    Advantages: Safe, uncomplicated, compatible with ISO functions like memcpy(). Compatible with 1-d arrays and functions accepting 1-d arrays.
    Disadvantages: Dimensions can't be set or changed in runtime. Possibly eats a lot of static memory. Somewhat slow access time since the index

    needs to be calculated.

    Dynamic pointer-to-pointer multi-array:

    [code]int** array;
    array = malloc(x * sizeof(int*));
    for(...) array[i] = malloc(y * sizeof(int));[/code]

    Advantages: Dimensions can be set and changed in runtime. Easy to change the size of the outer dimension. Allocated on the heap.
    Disadvantages: Slow and unsafe. Not compatible with ISO functions like memcpy(), or with static multi-arrays.

    Dynamic array pointer multi-array:

    [code]int (*array)[x];
    for(...) array[i] = malloc(x * sizeof(int));[/code]

    Advantages: Dimensions can be set and changed in runtime. Easy to change the size of the outer dimension. Allocated on the heap. Compatible with ISO functions like memcpy() and with static multi-arrays.
    Disadvantages: Slow and unsafe. Complicated syntax. Turns messy when more than 2 dimensions are used.
    (As you can see, this version beats pointer-to-pointer in every way.)


    Dynamic mangled multi-array:

    [code]int* array;
    array = malloc(x*y);[/code]

    Advantages: Dimensions can be set and changed in runtime. Allocated on the heap. Compatible with ISO functions like memcpy() and with static multi-arrays.
    Disadvantages: Slow and unsafe. Changing dimensions is inefficient.
Sign In or Register to comment.