-1

In the code below, the function calculo() doesn't return any values. It was supposed to return the arithmetic mean between cont and aluno variables, but it always returns 0.

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

int i;

int calculo(int alunos, int *p) {
    int cont = 0;
    for (i = 0; i < alunos; i++)
        cont += p[i];
    return cont / alunos;
}

int main()
{
    int *p, alunos, media;

    printf("Digite a quantidade de alunos na turma \n");
    scanf("%d", &alunos);

    p = (int*)calloc(alunos, sizeof(int));

    if (p == NULL) {
        printf("Memória Insuficiente");
        return 1;
    }
    srand(time(NULL));
    for (i = 1; i < alunos + 1; i++) {
        *p = rand() % 10;
        printf("Nota do Aluno [%d]: \t %d \n", i, *p);
    }

    media = calculo(alunos, p);
    printf("A Media Aritimetica da turma e:\t %d", media);
    getche();
    free(p);
    return 0;
}

What am I doing wrong?

  • What did you observe when inspecting the values using your debugger? You are aware how integer value division works? – πάντα ῥεῖ Oct 20 '18 at 20:55
  • Why is this question tagged as "C++", when it doesn't actually have a single line of C++ code, only a bunch of old, crufty, C? – Sam Varshavchik Oct 20 '18 at 20:56
  • 2
    @SamVarshavchik Found some C++ in line 4 ;) – Swordfish Oct 20 '18 at 20:57
  • Take a look at how you're reading in your numbers, and where you store them – 1201ProgramAlarm Oct 20 '18 at 21:02
  • The problem is only reading data into one element of `p`. Also be aware that your function does integer division for `cont/alunos`, which ALWAYS rounds toward zero. So the average of `1` and `2` (calculated as `(1 + 2)/2` in your function will give a result of `1`, not `1.5`. – Peter Oct 20 '18 at 21:14
  • @LeonardoJob Welcome to StackOverflow. As pointed out, your program is completely in C style and not C++...so the `#include ` is not used. It's very important on this site to distinguish in your questions whether you intend to use C style *(e.g. `printf("...")` and `malloc(...)` and `strcpy(...)`)* or C++ style *(e.g. `std::cout << "..."` and `new ...` and `std::string`)*, because it deeply affects the answers you will get. You should also know that conio.h is very old, [and not standard](https://stackoverflow.com/q/34474627/)--should be avoided in questions here. – HostileFork says dont trust SE Oct 20 '18 at 21:15
  • @HostileFork Thanks for all the help, there are so many things wrong that I didnt even know, thanks for pointing it out. – Leonardo Job Oct 20 '18 at 21:21
  • @LeonardoJob Sure thing. Be persistent, StackOverflow can sometimes be hard for new users...but it's just about the best way to get better that there is. If you want to learn a language, it can be very useful to monitor a tag and see the other questions go by. You learn fast--not just about the language, but about what makes questions get treated well vs. downvoted and complained about. Like I said, it helps to make a strong decision if you want to be pursuing [tag:c] or [tag:c++], maybe following those tags for a while would help you decide which is more your style. – HostileFork says dont trust SE Oct 20 '18 at 21:26

1 Answers1

3
srand(time(NULL));
for (i = 1; i < alunos + 1; i++) {
    *p = rand() % 10;  // <<=========================== here
    printf("Nota do Aluno [%d]: \t %d \n", i, *p);
}

you are assigning to *p aka p[0] over and over again. What you perhaps want to do is

srand(time(NULL));
for (i = 0; i < alunos; ++i) {
    p[i] = rand() % 10;
    printf("Nota do Aluno [%d]: \t %d \n", i + 1, p[i]);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • It worked, thank you so much!, But i dont think i get it. If p[i] stores the number inside the array, so what would *p store then?. – Leonardo Job Oct 20 '18 at 21:12
  • In C and C++ `something[index]` is just syntactic sugar for `*(something + index)` ... so `*p` which is the same as `*(p+0)` could be read as `p[0]`. – Swordfish Oct 20 '18 at 21:13