Search code examples
cprogramming-languages

Was told my C program was "hard coded" and I don't understand why


I turned in my assignment to my online C programming class and was docked huge due to the fact that my program was "hard coded, and I can't see how it would be considered "hard coded" as I ask for user input. The following was my code:

#include <stdio.h>
#include <stdlib.h>

#define IMAX 3
#define JMAX 4

int main()
{   
    float a[IMAX][JMAX];
    float avgrow[5];
    float avgcol[5];
    int i,j;
    char c;

    printf ("This program will allow you to enter numbers for 3 rows and 4 columns from         left to right then filling down, and take the averages of the rows and columns and list them next to the row and under the columns. You may use decimals but only 2 will display in the results. Press enter!\n");
    scanf ("%c",&c);

    printf("Enter 12 numbers here for your rows and columns:\n");
    for(i = 0; i < IMAX; i++)
    {
        for(j = 0; j < JMAX; j++)
        {
            scanf("%f",&a[i][j]);
        }
    }
    for(j = 0; j < JMAX; j++)
    {
        avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
        avgrow[1] = (a[1][0]+a[1][1]+a[1][2]+a[1][3])/JMAX;
        avgrow[2] = (a[2][0]+a[2][1]+a[2][2]+a[2][3])/JMAX;
    }
    for(i=0; i < IMAX; i++)
    {
        avgcol[0] = (a[0][0]+a[1][0]+a[2][0])/IMAX;
        avgcol[1] = (a[0][1]+a[1][1]+a[2][1])/IMAX;
        avgcol[2] = (a[0][2]+a[1][2]+a[2][2])/IMAX;
        avgcol[3] = (a[0][3]+a[1][3]+a[2][3])/IMAX;
    }

    printf("  Column1         Column2         Column3         Column4         Row Average\n\n");
    printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[0][0],a[0][1],a[0][2],a[0][3],avgrow[0]);
    printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[1][0],a[1][1],a[1][2],a[1][3],avgrow[1]);
    printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[2][0],a[2][1],a[2][2],a[2][3],avgrow[2]);
    printf("\n");
    printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t\n",avgcol[0],avgcol[1],avgcol[2],avgcol[3]);

    return 0;
}

All it was supposed to do was make a 2-d array with 3 rows and 4 columns, then take the average of the rows and display that next to to the row in a table. Than take the average of the columns and display them beneath the columns in the table.

This was his comments on my assignment "Well, you got the correct answers, but when dealing with a 2-D array, you should use nested for loops. Not one for loop and then a lot of "hard coding" values into the program."

Any help deciphering this would be appreciated as I though I was finally understanding programming until this.


Solution

  • First of all it's not meaningful to talk about a program being hard coded or not. Rather one would talk about specific values being hard coded. What this means is that you wrote their values directly into the code rather than putting them in a constant or variable that can easily be changed.

    In this case the values you hard-coded are the number of rows and the number of columns. You do have constants for these, but you don't use them consistently. That is if you changed your constants to turn the array into a 5x5 array, your code would now break because parts of the code would still act like it is an 3x4 array.

    Specifically there are two loops in your code where you're accessing the indices [0][0] through [2][3] by spelling out each index in that range specifically rather than using a loop. This means that if you change IMAX and JMAX, it will still use those same indices, which aren't correct any more.

    So your array indices are hard-coded and changing the array dimensions breaks your program.