简体   繁体   中英

Float pointer malloc array receiving into array

I am writing a code which requires me to use the malloc function, feed input into an array, determine and print the average as well as how each array value relates to the average. Here is my code

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

int main (){
  int i, n;
  float* numbers;
  float sum, average;
  sum = 0;
  printf("How many numbers? ");
  scanf("%d", &n);
  numbers = (int*)malloc(n * sizeof(int));
  printf("Enter %d decimal numbers\n", n);

for(i = 0; i < n; i++)
{

    scanf("%f", &numbers[i]);
    printf("%f\n", &numbers[i]);
    sum = sum + numbers[i];
}

printf("%f\n", &sum);
 average = sum / n;
 printf("The average was %f\n", &average);
 for(i = 0; i < n; i++)
    {
    if (numbers[i] < average)
        printf("%f is less than the average\n" , &average);
    else if (numbers[i] > average)
        printf("%f is less than the average\n" , &average);
    else
        printf("%f is average\n" , &average);
    }
    return 0;
}

The print statement in the very first for loop I was using to check whether or not my array is receiving the input properly, it is not, as shown by my output, which is below, my question is why is my array not receiving the values?

How many numbers? 4
Enter 4 decimal numbers
4
0.000000
4
0.000000
4
0.000000
4
0.000000
0.000000
The average was 0.000000
0.000000 is average
0.000000 is average
0.000000 is average
0.000000 is average

A few corrected issues:

1)

numbers = (int*)malloc(n * sizeof(int)); 

Allocation of memory for int was replaced with the allocation for float. Cast is not required.

numbers = malloc(n * sizeof(float));

2)

 printf("%f\n", &numbers[i]);

The intention is not to print a pointer to number[i] but the number[i] itself.

printf("%f\n", numbers[i]);

This type of issue was corrected for a few other printing places.

3) Final prints was corrected, logically program required printing the numbers[i] not the average

if (numbers[i] < average)
        printf("%f is less than the average\n" , numbers[i] );
    else if (numbers[i] > average)
        printf("%f is more than the average\n" , numbers[i] );

Corrected program:

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

int main (){
  int i,n;

  float* numbers;
  float sum, average;
  sum = 0;

  printf("How many numbers? ");
  scanf("%d", &n);

  numbers = malloc(n * sizeof(float));

  printf("Enter %d decimal numbers:\n", n);

  for(i = 0; i < n; i++)
  {
    scanf("%f", &numbers[i]);

    printf("%f\n", numbers[i]);

    sum = sum + numbers[i];
  }

  printf("%f\n", sum);

  average = sum / n;
  printf("The average was %f\n", average);

  for(i = 0; i < n; i++)
  {
    if (numbers[i] < average)
        printf("%f is less than the average\n" , numbers[i] );
    else if (numbers[i] > average)
        printf("%f is more than the average\n" , numbers[i] );
    else
        printf("%f is average\n" , average);
  }

  return 0;
}   

OUTPUT:

How many numbers? Enter 4 decimal numbers:
2
10
5
8
2.000000
10.000000
5.000000
8.000000
25.000000
The average was 6.250000
2.000000 is less than the average
10.000000 is more than the average
5.000000 is less than the average
8.000000 is more than the average

The problem is here:

printf("%f\n", &numbers[i]);

You're printing a reference to numbers[i] instead of the value itself. Try:

printf("%f\n", numbers[i]);

Same thing for all the other places you print values (ie sum , average ). You're getting 0's when you print it because when you take a pointer to one of those values and cast it to a float it is interpreting that as zero.

Perhaps someone else will be able to enlighten us an exactly why that is (ie I'm not sure if that is undefined behavior).

In case you're still confused: the reason you have to pass a reference ( & ) to the value to scanf , yet the value itself to printf , here's the reason: for scanf you're telling it "please get a float from the console and put it at this location I am specifying (ie &numbers[i] )". When you call printf , you are telling it "please print this value I am giving you (ie numbers[i] )".

sum=0f; Writing f so that it is interpreted as float constant instead of double.This doesn't matter much but is good practice and can help striking out a lot of problems.

numbers = (float*)malloc(n * sizeof(float)); Since you want a float array and not an integer array.

printf("%f\\n",numbers[i]); Use '&' only when you want to access the address of variable . To access the value stored in the variable simply use the variable name.

printf("Sum is : %f\\n", sum); Writing proper message in output is important.

for(i = 0; i < n; i++) { if (numbers[i] < average) printf("%f is less than the average\\n" , numbers[i]); else if (numbers[i] > average) printf("%f is less than the average\\n" , numbers[i]); else printf("%f is average\\n" , numbers[i]); }

You need to print the value of number[i] instead of average.

And remember not to use & in printf unless you want to print a address

First fix the malloc

numbers = malloc(n * sizeof(float)); // Not 'int'

then fix the for and next printf

printf("%f\n", numbers[i]); // printf wants a value not an address

printf("%f\n", sum);        // same

then

printf("The average was %f\n", average); // same

and the 3 next similar printf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM