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

Pages: 1-

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:23

/g/

Get out, ``Faggot''

Name: Anonymous 2011-11-24 18:27

I apologise, please help me.

Name: Anonymous 2011-11-24 18:29

>>3
And then I whisper: [spoiler]    NO   

Name: Anonymous 2011-11-24 18:30

fuck

Name: Anonymous 2011-11-24 18:31

#include <stdio.h>

int main(int argc, char** argv)
{
    FILE* input = NULL;
    char grades[10][4];
    int i, j;
    int low = 0, medium = 0, high = 0;

    /* print usage if no input argument */
    if(argc < 2){
        printf("usage: %s input.txt\n", argv[0]);
        return 0;
    }

    /* open input file */
    input = fopen(argv[1], "r");
    if(!input){
        printf("Could not load text file\n");
        return 0;
    }

    /* read in the grades */
    for(i = 0; i < 10; i++)
        for(j = 0; j < 4; j++)
            fscanf(input, "%c", &grades[i][j]);

    /* look at the "brackets" */
    for(i = 0; i < 10; i++)
        for(j = 0; j < 4; j++)
            if(grades[i][j] <= 40)        ++low;
            else if(grades[i][j] > 70)    ++high;
            else                ++medium;

    /* show results */
    printf("There were %d low grades, %d average grades, and %d high grades\n",
        low, medium, high);

    /* close input file */
    fclose(input);

    return 0;
}

Name: Anonymous 2011-11-24 18:34

Whoops, typo.
fscanf(input, "%d", &grades[i][j]);

Name: Anonymous 2011-11-24 18:36

Also
int grades[10][4];. Sorry I'm a bit retarded today.

Name: Anonymous 2011-11-24 18:37

>>6,7

you aren't helping him by providing code for him to copy paste, and segmentation fault

Name: Anonymous 2011-11-24 18:38

>>6

That's amazing, and it's the best way to go about it? The lecturer and his preceptors are awkward bastards and will mark you down for little things, I don't want to give them excuses.

Name: Anonymous 2011-11-24 18:39

>>9

I could already do this, but I thought that there might have been a better way to go about it, using pointers or something like that. If this is the best way then surely they'll be happy with it.

Name: Anonymous 2011-11-24 18:40

>>9
I think >>8 fixes the segfault. And I guess OP can look at the code above as an example only, as I am now claiming copyright on it.

Name: Anonymous 2011-11-24 18:41

>>11
I think indeed the best way is the simplest way.

Name: Anonymous 2011-11-24 18:44

Give me a minute. There's a way that doesn't require nested loops. See if your instructions are able to figure out how.

Name: Anonymous 2011-11-24 18:48

>>14

Waiting for it

Name: Anonymous 2011-11-24 18:49

Well >>6 can be written more "flatly" as #include <stdio.h>

int main(int argc, char** argv)
{
    FILE* input = NULL;
    int grades[40];
    int i;
    int low = 0, medium = 0, high = 0;

    /* print usage if no input argument */
    if(argc < 2) {
        printf("usage: %s input.txt\n", argv[0]);
        return 0;
    }

    /* open input file */
    input = fopen(argv[1], "r");
    if(!input) {
        printf("Could not load text file\n");
        return 0;
    }

    /* read in the grades */
    for(i = 0; i < 10 * 4; i++)
            fscanf(input, "%d", &grades[i]);

    /* look at the "brackets" */
    for(i = 0; i < 10 * 4; i++)
            if(grades[i] <= 40)        ++low;
            else if(grades[i] > 70)    ++high;
            else                         ++medium;

    /* show results */
    printf("There were %d low grades, %d average grades, and %d high grades\n",
           low, medium, high);

    /* close input file */
    fclose(input);

    return 0;
}
. Otherwise if the subgroup matters I guess you can always do an integer division by 4 to find out in which group of 4 datum i is.

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

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