Ibrahim
Ibrahim

Reputation: 37

"assignment makes integer from pointer without a cast"?

I'm really new to this. I've never done anything like this so I'm having issues with this code. I was given a template to write my code in separate functions like this, although I added the findPos one myself. I'm getting the "assignment makes integer from pointer without a cast" warning and also my max, min, sum, avg, and position of max and min are obviously not coming out to the right numbers. I was just wondering if anyone can lead me in the right direction.

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

int findMin(int arr[], int size);
int findMax(int arr[], int size);
int findSum(int arr[], int size);
int findPos(int arr[], int size);
int size;
int i;
int max;
int min;
int avg;
int sum;
int pos;

int main()
{
  srand(time(0));

  printf("Enter an integer: ");
  scanf("%d", &size);

  int arr[size];

  max = findMax;
  min = findMin;
  pos = findPos;
  sum = findSum;
  avg = sum / size;

  printf("max:%7d\tpos:%d\t\n", max, pos);
  printf("min:%7d\tpos:%d\t\n", min, pos);
  printf("avg:%7d\n", avg);
  printf("sum:%7d\n", sum);

  printf("\n");

  printf(" Pos  :   Val\n");
  printf("-------------\n");

  for (i = 0; i < size; i++) {
    arr[i] = (rand() % 1001);
    printf("%4d  :%6d\n", i, arr[i]);
  }
    return 0;
}

int findMin(int arr[], int size)
{
  min = arr[0];
  for (i = 0; i < size; i++) {
    if (arr[i] < min) {
      min = arr[i];
    }
  }

  return min;
}

int findMax(int arr[], int size)
{
  max = arr[0];
  for (i = 0; i < size; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }

  return max;
}

int findSum(int arr[], int size)
{
  sum = 0;
  for (i = 0; i < size; i++) {
    sum = sum + arr[i];
  }

  return sum;
}

int findPos(int arr[], int size)
{
  for (i = 0; i < size; i++) {
    pos = i;
  }

  return pos;
}

Upvotes: 1

Views: 761

Answers (2)

Saleel Ahsan K
Saleel Ahsan K

Reputation: 11

There are a couple of issues with the code. Let me iterate through the same

Populating Data in Created Array

Since the data has to present the created array before performing any operations,

 printf("\n");

  printf(" Pos  :   Val\n");
  printf("-------------\n");

  for (i = 0; i < size; i++) {
    arr[i] = (rand() % 1001);
    printf("%4d  :%6d\n", i, arr[i]);
  }

this snippet should be reordered and moved above the function calls and just after the int arr[size];

Function Calls

All your functions, namely findMax,findMin,findPos,findSum is expecting two parameters

  1. arr - array you have created
  2. size - the size value read from scanf()

Assuming you want to store the return value from the function in the main int variables max,min,pos,sum,avg

the statements

max = findMax;
min = findMin;
pos = findPos;
sum = findSum;

should be replaced with function calls like

max = findMax(arr, size);
min = findMin(arr, size);
pos = findPos(arr, size);
sum = findSum(arr, size);

The Final Main code will be

int main()
{
  srand(time(0));

  printf("Enter an integer: ");
  scanf("%d", &size);

  int arr[size];

 printf("\n");

  printf(" Pos  :   Val\n");
  printf("-------------\n");

  for (i = 0; i < size; i++) {
    arr[i] = (rand() % 1001);
    printf("%4d  :%6d\n", i, arr[i]);
  }

  max = findMax(arr, size);
  min = findMin(arr, size);
  pos = findPos(arr, size);
  sum = findSum(arr, size);
  avg = sum / size;

  printf("max:%7d\tpos:%d\t\n", max, pos);
  printf("min:%7d\tpos:%d\t\n", min, pos);
  printf("avg:%7d\n", avg);
  printf("sum:%7d\n", sum);
 
    return 0;
}

Upvotes: 0

MarkSouls
MarkSouls

Reputation: 999

max = findMax;
min = findMin;
pos = findPos;
sum = findSum;

You're assigning function pointer, not return value, to integer variable. You have to do something like max = findMax(arr, size). Also in that case, you should assign values to arr before calling it.

Upvotes: 1

Related Questions