-1

I'm starting to learn programming in C, and I have this task where I have to write a program with part of the code on another file. But I'm having problems with that last part because I'm using matrices.

Here's the main body:

#include <stdio.h>
#include "otsenkatry.c"

int main()
{
    int i, j;
    int a[i];
    int s, gru;
    char A, B, C, D, E;

    printf("Introduce the number os students ", &s);
    fflush(stdout);
    scanf("%d", &s);
    printf("Introduce their grades\n");
    fflush(stdout);

    for (i = 0; i<s; i++)
    { 
        printf("a[%d] = ", i); 
        fflush(stdout);
        scanf("%d", &a[i]);

        printf("Grade: %d  %d \n", a, otsenkatry(a));
        fflush(stdout);//}
    }

    return 0;                       
}

And that's the part with the problem:

int otsenkatry (int* a)
{
    int i;
    int gru;

    if (a[i]<51)
    {   
        gru=2;
    }
    if (a[i]>50 && a[i]<69)
    {
        gru=3;
    }
    if (a[i]>69 && a[i]<=85)
    {
        gru=4;
    }
    if (a[i]>85 && a[i]<=100)
    {
        gru=5;
    }

    return gru;                         
}

I figured, that it has to do with the pointers, but I don't know how to alter it.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
EvMen
  • 1
  • 1
  • Please indent your code properly. This is impossible to read – Shloim Jan 06 '20 at 08:27
  • 1
    Show your build command – M.M Jan 06 '20 at 08:30
  • There are several flaws in your code. Have a look at a [related post](https://stackoverflow.com/a/21652977/1911064) and look-up the usage of 'C' header files. – Axel Kemper Jan 06 '20 at 08:33
  • There is a problem with the instruction `printf("Grade: %d %d \n", a, otsenkatry(a));`. `a` is an array and you expect it to print as an integer. I suppose you meant `printf("Grade: %d %d \n", a[i], otsenkatry(a));` – chmike Jan 06 '20 at 08:45
  • Your title says "undefined reference to WinMain". How do you get to assumption "Something with the pointers"? Are there any other error messages before that missing reference message? – Gerhardh Jan 06 '20 at 10:54
  • If your problem is still not solved (maybe because you mixed 2 problems in 1 question) you could answer the questions other people have asked in the comments. – Gerhardh Jan 07 '20 at 15:48

2 Answers2

0

Your matrix has undefined size:

int i, j;
int a[i];

To declare matrix a[] properly you need to pass the size - the value of i variable. Unfortunately, the i variable is declared one line above without initialization with any value.

VillageTech
  • 1,968
  • 8
  • 18
  • If I declare `i` later then I get `[Error] 'i' and 'a' was not declared in this scope`. How can I declare the value of the variable there if its "unknown" (depends on the variable `s`)? – EvMen Jan 06 '20 at 09:18
  • I can't understand how are you going to learn C without reading some background knowledge... There is no possibility to learn programming using only experiments... And especially programming in C. – VillageTech Jan 06 '20 at 09:27
0

There are a few problems with your code:

  • array a not properly declared
  • printing array a instead of integer
  • argument of otsenkatry is array, but should be an int
  • including a .c file
  • using undefined i value as array index in otsenkatry
  • the argument &s in the first printf is invalid
  • the otsenkatry function can be simplified
  • the variables j, gru, A, B, C, D, E are defined in main but never used

Here is a corrected implementation:

#include <stdio.h>


int otsenkatry (int v) {
    if (v<51)
        return 2;
    if (v<69)
        return 3;
    if (v<=85)
        return 4;
    if (v<=100)
        return 5;
    return 0;
}


int main(){
    int i, a[100], s;

    printf("Introduce the number of students ");
    fflush(stdout);
    scanf("%d", &s);
    if (s > 100)
        s = 100;
    printf("Introduce their grades\n");
    fflush(stdout);

    for (i = 0; i<s; i++) { 
        printf("a[%d] = ", i); 
        fflush(stdout);
        scanf("%d", &a[i]);

        printf("Grade %d: %d \n", a[i], otsenkatry(a[i]));
        fflush(stdout);
    }

    return 0;                       
}
chmike
  • 20,922
  • 21
  • 83
  • 106
  • @EvMen WinMain is a windows specific issue. I can’t help you on that. It may depend on the compiler you use. – chmike Jan 07 '20 at 09:10
  • Thank you, it works, although the "undefined reference to 'WinMain' [Error] ld returned 1 exit status" still appears. – EvMen Jan 07 '20 at 09:15