Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Best way to do this

Name: Alex 2011-11-24 18:12

Hey /g/. I'm doing an assignment and it asks me to read in a bunch of integers from a text file into an array (marks for assignments). I have an array[10][4], 10 students with 4 grades each.

What I want to do is check the value of these grades, and if they're say <=40, then do something, if they're >70, do something. I need to count up the number of marks in each bracket. I could easily do this using for loops in the main function, but it seems very messy and we're marked on the way we implement the program rather than simply solving the problem.

I tried to use the array as a parameter to a function, and then use the function to check the values, but I couldn't get this to work. Could I use pointers to do this? Jus tell me the best way to do it /g/, I can try to implement it myself. The language is C, by the way.

Name: Anonymous 2011-11-24 18:52

>>12

Good, that is the proper way.

>>11

If you are using arrays, you are technically using pointers. That is, there is an equivalence:

a[i] = 3;
*(a + i) = 3;

Both lines do the same thing. But if you are doing array like operations, it is better to us indexing instead of pointer arithmetic.

Name: Anonymous 2011-11-24 19:06

>>16

Question specifies that I have to use a 2D array, sorry. I agree with you though, it's a much better way to do it.

Name: Anonymous 2011-11-24 19:12

You normally don't want to use fscanf, but here. Make a sample grade file and test it.


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

#define DATA_SIZE 4*10

int main(int argc, char** argv){
                if (argc < 2){
                                fprintf(stderr, "Parameter: <grade-file.txt>\n");
                                return 1;
                }

                FILE *grades = fopen(argv[1], "r");

                if (!grades){
                                fprintf(stderr, "Unable to open %s\n", argv[1]);
                                return 1;
                }

                int *store = malloc(sizeof(int)*DATA_SIZE), *finger;
                int low, medium, high, i;

                low = medium = high = 0;



                for (i = 0, finger = store; i < DATA_SIZE; ++i, ++finger){
                                fscanf(grades, "%d", finger);
                                if (*finger <= 40) ++low;
                                else if (*finger >=70) ++high;
                                else ++medium;
                }

                fprintf(stdout, "%d, %d, %d\n", low, medium, high);

                fclose(grades);

                return 0;
}

Name: Anonymous 2011-11-24 19:14

>>16

Question specifies that I have to use a 2D array, sorry. I agree with you though, it's a much better way to do it.

Name: Anonymous 2011-11-24 19:19

>>19

Damn, sloppy. I forgot to free(store);

>>20

You can make this code into a 2D array easily. Just divide the memory pointed to by int *store; by the number of students and traverse it by that much at a time.

Arrays and pointers are the same, as >>17-san says.

Name: Anonymous 2011-11-24 19:23

check 'em

Name: Anonymous 2011-11-24 19:32

>>20

Case in point:


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

#define DATA_SIZE 4*10

int main(int argc, char** argv){
                if (argc < 2){
                                fprintf(stderr, "Parameter: <grade-file.txt>\n");
                                return 1;
                }

                FILE *grades = fopen(argv[1], "r");

                if (!grades){
                                fprintf(stderr, "Unable to open %s\n", argv[1]);
                                return 1;
                }

                int *store = malloc(sizeof(int)*DATA_SIZE), *finger;
                int low, medium, high, i;

                low = medium = high = 0;



                for (i = 0, finger = store; i < DATA_SIZE; ++i, ++finger){
                                fscanf(grades, "%d", finger);
                                if (*finger <= 40) ++low;
                                else if (*finger >=70) ++high;
                                else ++medium;
                }

                fprintf(stdout, "%d, %d, %d\n", low, medium, high);

                int j, k;

                for (j = 0; j < 10; ++j){
                                for (k = 0; k < 4; ++k){
                                                fprintf(stdout, "%d\n", (&store[j])[k]);
                                }
                }


                fclose(grades);
                free(store);

                return 0;
}


Store is treated as a 2D array, you can also cast it to one.

Name: Anonymous 2011-11-24 19:55

They will probably mark you down if you are too verbose. Lecturers hate reading through page after page of inefficient newb code.
Keep it short and sweet, with a twang:

#include <stdio.h>
void main(int argc, char** argv) {
    char i=0, j=0, grades[10][4];int ranks[3]={};
    for (FILE* f = fopen(argv[1],"r"); fscanf(f,"%hhd",&grades[i][j])==1 && ++ranks[(int)((3-((100-grades[i][j])/30))*0.9)]||fclose(f)&&0;j=(j+1)&3,i+=!j);
    printf("There were %d low grades, %d average grades, and %d high grades\n", *(struct{int a,b,c;}*)ranks);
}

Name: Anonymous 2011-11-24 20:07

>>24

Turn this in OP.

Name: Anonymous 2011-11-24 23:12

If it ain't Lisp, it's crap.

Name: Anonymous 2011-11-25 1:50

>>24
Hahaha, awesome.

Name: Anonymous 2011-11-25 10:02

giving an answer to a homework question
not in haskell

Wow, it's like I'm not in /prog/ anymore.

Name: Anonymous 2011-11-25 11:00

>>28
Fuck off back to /g/.

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2011-11-25 11:09

#include <fcntl.h>
#include <stdio.h>
#define STGRADES 10*4
main(int argc, char** argv){
int low = 0,med =0,high=0,i=-1,grades[STGRADES];
read(open(argv[1],O_RDONLY),&grades,STGRADES<<2);
while(++i<STGRADES)grades[i]<41?++low:(grades[i]>70?++high:++med);
printf("There were: %d low grades, %d average grades, and %d high grades\n", low, med, high);}

Name: Anonymous 2011-11-25 14:06

>>30

lol

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List