Home > Mobile >  Confusion in arrays
Confusion in arrays

Time:09-15


Recently I was learning about arrays passing to functions (by passing their base address to a pointer defined as parameter in function and then using pointer arithmetic for extracting the whole array subsequently)
For practice I was asked to calculate the average marks of a class of 70 students with their marks listed in an array named "marks" and was asked to define a variable with parameter as a pointer and calculate average from there.
The data given to me was that student 1 scored 40 , student 2 scored 41, student 3 scored 42....and so on.

Here is my attempt at it:

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

float average(int *b)
{
    int sum = 0;
    for (int i = 1; i <= 70; i  )
    {
        sum = sum   *b;
        b = b   1;
    }
    printf("the value of sum is %d\n", sum); // this value is changing every time I run the program
    return (((float)sum) / 70);
}

int main()
{
    int marks[70];
    marks[0] = 40;

    for (int i = 0; i < 68; i  )
    {
        marks[i   1] = marks[i]   1;
    }

    printf("the value of marks of 10th child is %d\n", marks[9]); // Just for checking if I am correct!(yes! the ans does come out to be 49!)
    printf("the value of average marks of the class is %f\n", average(&marks[0]));

    return 0;
}

to my surprise the value kept changing every time I ran it. Can anyone give a hint where am I wrong?

CodePudding user response:

You're problem is related to the fact (as mentioned in my comment) that your array is uninitialized.

The memory for it has been allocated but its still random jumble data. Luckily you overwrite that data for all entries in the array, except for the last one. The last entrance is basically a random value at this point.

So thats the reason the output keeps changing, your actuall bug is a bit simpler.

In the for loop where you calculate the sum, you iterate from i = 0 to i = 67. So with the 1 offset you change all the entries from 1 to 68, so the last entrance (marks[69]) doesn't get touched.

Fixed code:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float average(int *b) {
  int sum = 0;
  for (int i = 1; i <= 70; i  ) {
    sum = sum   *b;
    b = b   1;
  }
  printf("the value of sum is %d\n",
         sum); // this value is changing every time I run the program
  return (((float)sum) / 70);
}

int main() {
  int marks[70];
  marks[0] = 40;
  for (int i = 0; i < 68; i  ) {
    marks[i   1] = marks[i]   1;
  }
  printf("the value of marks of 10th child is %d\n",
         marks[9]); // Just for checking if I am correct!(yes! the ans does come
                    // out to be 49!)
  printf("the value of average marks of the class is %f\n", average(&marks[0]));

  return 0;
}

PS: In the average function ,you use pointer arithmetic to loop over the input array, which is considered bad practice by a lot of people. Also, youre basiaclly not using the for-loop incrementor variable you create (int i). A easier and safer way to do this is :

float average(int *b) {
  int sum = 0;
  for (int i = 0; i < 69; i  ) {
    sum  = b[i];
  }
  printf("the value of sum is %d\n",
         sum); // this value is changing every time I run the program
  return (((float)sum) / 70);
}
  • Related