I'm currently trying to create a small program that gets the user to enter 5 values, which are then saved in an array, and then the program calculates the average and displays it.

Here is what I have so far, it works as far as getting the user to enter the values however I have no idea how to get it to find out the sum of the values and then calculate the average. I have put in what I thought it should be but it is throwing up the following errors when I compile it.

'+' : pointer addition requires integral operand
'=' : cannot convert from 'float' to 'int (__cdecl *)(const char *,...)'

Can somebody please point me in the direction of a solution.

Thanks very much.

#include <stdio.h>

int main()
{
    char str[5][20];
    int i;
	float sum;
	float average;


    for (i=0;i<5;i++)
    {
        printf("\nEnter value %d:",i+1);
        fgets(str[i],20,stdin);
    }
 

		printf("Item # - Value");

    for(i=0;i<5;i++)
    {
        printf("\n%d\t - %s",i+1,str[i]);
    }
//problem start
	for(i=0;i<5;i++)
	{
		sum = sum + str[i];
	}
	
		average = sum/5;
		printf=("%f",average);
//problem end
	return 0;
}

You are inputting strings and you want the average of them? I guess first you need to explain this:

What is the average of "John", "Book", "Table", "Sheila" and "Europe"?

Answer that, and we can understand what you need...

Sorry, was being retarded. I copied the first few lines from an old project and didn't check that.

The code below should be less retarded but it is showing an error on line 27, saying:

'+' : pointer addition requires integral operand

As far as I can tell I have used floats for the inputs into the array, the sum and the average, which should allow it to work. I have no idea what is wrong now.

#include <stdio.h>

int main()
{
    float str[5][20];
    int i;
	float sum;
	float average;


    for (i=0;i<5;i++)
    {
        printf("\nEnter the weight of person %f:",i+1);
        fgets(str[i],20,stdin);
    }
 

		printf("Person # - Weight");

    for(i=0;i<5;i++)
    {
        printf("\n%d\t - %f",i+1,str[i]);
    }

	for(i=0;i<5;i++)
	{
		sum = sum + str[i];
	}
	
		average = sum/5;
		printf("%f",average);

	return 0;
}

Please ignore my last post.

I have changed the data types so they are uniform all the way through, so the initial input is being read as a float and when it is being used in the sums towards the end it is read as a float, but it still isn't working.

The main problem area is the large comment towards the bottom. One more possible problem is with the displaying of the values. Where I have put the first comment, if I tell it to print a float then every row simply comes out as 0.00000000, it only works if I define is as a string. Is this a problem?

The code currently looks as follows:

#include <stdio.h>

int main()
{
    float str[5][20];
    int i;
	float sum;
	float average;


    for (i=0;i<5;i++)
    {
        printf("\nEnter value %d:",i+1);
        fgets(str[i],20,stdin);
    }
 

		printf("Item # - Value");

    for(i=0;i<5;i++)
    {
        printf("\n%d\t - %s",i+1,str[i]);//is it OK to use %s here?
    }

	/*for(i=0;i<5;i++)
	{
		sum = sum + str[i];
	}
	
		average = sum/5;
		printf=("%f",average);
		*/
	return 0;
}

Thanks for any help.

Your code doesn't make sense. Why are you using fgets() on floats? Try setting up like this.

#include <stdio.h>

#define ARR_SIZE 5

int main()
{
	float nums[ARR_SIZE];
	int i = 0;

	for ( i = 0; i < ARR_SIZE; i++ )
	{
		printf("\nEnter value %d:", i + 1);
		fscanf(stdin, "%f", &nums[i]);
	}


	for(i = 0; i < ARR_SIZE; i++)
	{
		printf("%d - %f\n",(i + 1), nums[i]);
	}

	return 0;
}

hey buddy i wont help you much .. coz iam learning c as well i know is kinda hard but you should clean up your code .. make you variable name clear , its confusing , also fgets???? -thats for string dunno if that works for integers .. i recommend you to use scanf command, also dont forget your & ,
Well maybe you should try something easier for beginning try it with single array , here is a HINT for you
n stands for ary you want to work with
for(i=0,i=total;i<n;i++)
total=total+n
printf ("avearge is %d", total/i)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.