Help me out in C :)

Started by Zexion, November 20, 2012, 03:05:10 pm

Previous topic - Next topic

Zexion

Quote from: KK20 on November 20, 2012, 04:47:16 pm
QuoteI need to write a c program that takes an input from a file (input.dat) calculates sum and average of rows and columns and displays it on screen and in an output file (result.out).

Well that's misleading ._.


Yeah those were the original instructions she gave, but I think I mentioned somewhere in this thread that she said we could use arrays instead of reading a file.

Blizzard

That's why I gave that pseudo code in the beginning. If you're working with a dynamic array, it's much more complicated. But even a file with strictly 3x9 elements could be loaded easily.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

Quote from: Zexion on November 20, 2012, 05:02:11 pm
Yeah those were the original instructions she gave, but I think I mentioned somewhere in this thread that she said we could use arrays instead of reading a file.


Either way, the sum and average part works all the same, since it's still a matrix

Quote from: Blizzard on November 20, 2012, 05:40:25 pm
That's why I gave that pseudo code in the beginning. If you're working with a dynamic array, it's much more complicated. But even a file with strictly 3x9 elements could be loaded easily.

It's true that it's much more complicated but he wouldn't need to initialize the matrix in the code and specially wouldn't need to know its size beforehand. Your first code would do it nicely, and would work for any file - obviously following the given data's format - .
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Zexion

I was able to finish it, by the way. I had to rewrite somethings and dumb it down a bit so it can look like I did it lol. I think it looks pretty different though and also there were problems calculating the sum of the last 2 columns and the average of them as well, but over all it wasn't much to fix.

This was my final code incase anyone was interested.

#include <stdio.h>
/* Introduction to Computer Science CSEN 2304
* Assignment 3
* Due Tuesday, November 20
*/

main()
{
double input[9][3] ={ {-0.043200, -0.003471, 0.000000},
                  {-0.040326, -0.004851, -0.000737},
                  {-0.018204, -0.004246, -0.001530},
                  {0.022249, 0.008891, 0.004870},
                  {0.074892, 0.044237, 0.032171},
                  {0.129600, 0.100233, 0.089016},
                  {0.174747, 0.160100, 0.161792},
                  {0.200242, 0.199106, 0.214417},
                  {0.174747, 0.160100, 0.161792} };
           FILE *outp;
           int i, j;
       double rowSums[9] = {0.0};
       double columnSums[3] = {0.0};
           double sum,avg;
           outp = fopen("result.out", "w");
           
/* Draws the heading */
    printf("    ");
    fprintf(outp, "    ");
for (i = 0; i < 3; i++)
{
        printf("        ");
fprintf(outp, "        ");
printf("#%d  ", i);
fprintf(outp, "%d  ", i);
}
    printf("       Sum     Average\n");
    fprintf(outp, "       Sum     Average\n");
    lines();
   
/* Loop to calculate */
for (i = 0; i < 9; i++)
{
printf("%2d", i + 1);                 // Shows current row
fprintf(outp, "%2d", i + 1);
for (j = 0; j < 3; j++)
{
rowSums[i] += input[i][j];
columnSums[j] += input[i][j];
printf("   %9f", input[i][j]);    //Shows the inputs
fprintf(outp, "   %9f", input[i][j]);
}
printf("   %9f   %9f", rowSums[i], rowSums[i] / 3);
fprintf(outp, "   %9f   %9f", rowSums[i], rowSums[i] / 3);
sum += rowSums[i];
avg += rowSums[i] / 3;
printf("\n");
fprintf(outp, "\n");
}
        lines();
        printf("sum");
        fprintf(outp, "sum");
for (i = 0; i < 3; i++)
{
printf("  %9f ", columnSums[i]);
        fprintf(outp, "  %9f ", columnSums[i]);
}
/* Prints the sum row */
    printf("  %9f ", sum);
    fprintf(outp, "  %9f ", sum);
    printf("  %9f", avg);
    fprintf(outp, "  %9f", avg);
    printf("\n");
    fprintf(outp, "\n");
        lines();
    printf("avg.");
    fprintf(outp, "avg.");
   
    for (i = 0; i < 3; i++)
{
printf(" %9f  ", columnSums[i] / 9);
        fprintf(outp, " %9f  ", columnSums[i] / 9);
}   
    printf(" %9f  ", sum / 9);
    fprintf(outp, " %9f  ", sum / 9);
    printf(" %9f\n\n", avg / 9);
    fprintf(outp, " %9f\n\n", avg / 9);
   
        system("PAUSE");
}

lines(int i) {
           FILE *outp;
           outp = fopen("result.out", "w");
        printf("     ");
        fprintf(outp, "     ");
        for (i=0; i <=4; i++)
        {
        printf("---------   ");
        fprintf(outp, "---------   ");
        }
        printf("\n");
        fprintf(outp, "\n");
        }


If you actually run it, you'll notice that the .exe runs perfectly, but the file that is written doesn't. I'm not to sure why this happened, but it did. I wasn't able to fix it before I went to my chem study session for the night.

Blizzard

You forgot to call "fclose(outp);". If you don't call it, the last flush doesn't happen and the file is not closed.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zexion

Ah yeah :( oh well, it's the best I could do last minute lol. I was really happy that everyone responded so quickly. Oh and by the way, the code isn't spaced weirdly  like that, but for some reason it came out like that in the code box.