Problem is:Write a function that returns the average value of the elements in the array.
This is a solved problem, however I need to solve it via function, and not sure how to do it..Can anyone help please?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[20], num, i; //array declaration
double avg = 0, sum = 0; //variable declaration
printf("Enter the numbers of average: ");
scanf("%d", &num); //get inpur from user to numberof elements
printf("Enter the numbers: \n");
for (i = 1; i <= num; i++)
{ //loop for get input numbers
scanf("%d", &arr[i]);
}
for (i = 1; i <= num; i++)
{
sum = sum + arr[i]; //loop for calculating sum
avg = sum / num; //calculate average
}
printf("Average of entered numbers are: %f", avg);
getch(); //display result on the screen
return 0;
}
A function like this should help:
double calc_average(const int *array, size_t num_elements)
{
double sum = 0.0;
size_t i;
for(i = 0; i < num_elements; i++)
{
sum += array[i];
}
return (sum / num_elements);
}
with this prototype:
double calc_average(const int *array, size_t num_elements);
which you can call with:
avg = calc_average(arr, num);
but you'll need to fix your input loop in main() to start at 0:
for (i = 0; i < num; i++)
and also in main() you should make sure you don't overrun the bounds of your array:
if(num > 20)
{
fprintf(stderr, "Error: too many numbers\n");
return 1;
}
This is simple code. Hopefully, I didn't make any simple mistakes.
Related
How can I find the element closest to average in c array. This passes most tests however when I type in the sequence 4-1-5-7-2 it prints out 2, instead of 5.
#include <stdio.h>
#include <math.h>
int main() {
int A[50], n, k = 0, i = 0;
double avg = 0;
scanf_s("%d", &n);
do {
scanf_s("%d", &A[i]);
avg += (double)A[i] / n;
} while (++i < n);
printf("\n%f\n", avg);
for (i = 1; i < n; i++) {
if (fabs(A[k] - avg) > fabs(A[i]) - avg) {
k = i;
}
}
printf("%d", A[k]);
return 0;
}
In your if statement, replace fabs(A[i]) - avg with fabs(A[i] - avg) to get the absolute difference.
#include <stdio.h>
int sum_even(int n);
int sum_odd(int m);
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
int evensum;
evensum = sum_even(int n);
int oddsum;
oddsum = sum_odd(int m);
printf("the sum of even numbers is %d", evensum);
printf("the sum of odd numbers is %d", oddsum);
return 0;
}
int sum_even(int n) {
int sum = 0, i;
for (i = 2; i <= n; i += 2) {
sum += i;
}
return sum;
}
int sum_odd(int m) {
int SUM = 0, j;
for (j = 1; j <= m; j = j + 2) {
SUM = SUM + j;
}
return SUM;
}
please tell me what is wrong with my code, I am new to coding, I am able to solve questions without using functions but I am confused when I have to use functions
While calling functions you just pass the arguments and not specify its data type.
i.e. You would write it like this :-
evensum = sum_even(n);
oddsum = sum_odd(m);
The general syntax of calling a function which returns a value is :-
return-value = function-name(arg-list);
The function that is commented out is my function, the other is the one I picked from internet. There are no errors. The code just asks for 5 numbers, then asks to operate one of 4 operations, here (s) being for standard deviation, and then runs that function.
From what I've troubleshooted, it isn't reading the elements of the array. It isn't adding, removing, or doing anything with them. It just prints out a zero when asked to print something else in that function.
I'm a newbie, as is implied by the type of question I'm solving, but it'd be great if somebody could help me out here.
Thanks!
#include<stdio.h>
#include<math.h>
int arr[5];
int curnum, i; i = 0;
char op;
int incount; incount = 5.0;
int main();
int main(){
for (i=0; i<incount; i++)
{
printf("Enter a number : ");
scanf("%d", &arr[i]);
}
printf("What would you like to calculate? Average(a), Max(M), Min(m), Std. Dev.(s) : ");
scanf(" %c", &op);
float average(int arr[]);
float std(int arr[]);
switch (op){
case 'a':
printf("The average is %f \n", average(arr)); break;
case 'M':
printf("The Max number is %d \n", max(arr)); break;
case 'm':
printf("The Min num is %d \n", min(arr)) ; break;
case 's':
printf("the Standard Deviation is %f \n", std(arr)); break;
default:
printf("Invalid function \n"); break;
}
return 0;
}
float average(int arr[]){
float netsum;
netsum = 0;
for (i=0; i<incount; i++)
{
netsum = netsum + arr[i];
}
return (netsum/5);
}
int max(int arr[]){
int maxnum;
maxnum = arr[0];
for (i=0; i<incount; i++)
{
if (arr[i] > maxnum) maxnum = arr[i];
}
return maxnum;
}
int min(int arr[]){
int minnum;
minnum = arr[0];
for (i=0; i<incount; i++)
{
if (arr[i] < minnum) minnum = arr[i];
}
return minnum;
}
// float std(int arr[]){
// float currsq, sqsum, stdeviation; currsq = 0; sqsum = 0;
// for (i=0; i<incount; i++){
// currsq = arr[i] - average(arr);
// currsq = currsq*currsq;
// sqsum = sqsum + currsq;
// }
// stdeviation = pow (sqsum/incount, 0.5);
// return stdeviation;
// }
float std(int arr[]) {
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += arr[i];
}
mean = sum / 10;
for (i = 0; i < 5; ++i)
SD += pow(arr[i] - mean, 2);
return sqrt(SD / 10);
}
You are trying to dealing with 10 elements in your function std() while there are only 5.
You should use incount instead of the magic number.
float std(int arr[]) {
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < incount; ++i) {
sum += arr[i];
}
mean = sum / incount;
for (i = 0; i < incount; ++i)
SD += pow(arr[i] - mean, 2);
return sqrt(SD / incount);
}
Magic number is also used in the function average, but it didn't cause harm because 5 is used.
Also note that
int incount; incount = 5.0;
is not the right way to set initial value of gloval variables.
It should be
int incount = 5.0;
Same thing applies to the global i.
I'm sorry, absolutely noobie mistake. As I said from my testing, it wasn't taking any values from the array. That was because I didn't create the Variable (i) inside the function that was being used in the for loop.
I just added
int i;
inside the function, and voila.
Basically, the max size of the array is 10 and the user is allowed to enter up to 10 values. If the user enters -1 or 0, before entering the ten values, then the loop stops and goes to the next loop. My problem is is that it works perfectly UNTIL I enter 10 values. The result will divide by 9 instead of 10 and print that there are 9 values in the array.
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
count++;
}
n = count-1;
float sum = 0;
float average;
for(i = 0; i<=n; i++)
{
sum = sum + numbers[i];
}
average = sum/count;
printf("The average price of the %d products is %.2f.\n", count, average);
return 0;
} //this is the fixed solution.
You should not write n=count-1; write n=count; instead. And move count++ to the end of loop.
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
count++;
}
n = count;
float sum = 0;
float average;
for(i = 0; i<n; i++)
{
sum = sum + numbers[i];
}
average = sum/a;
printf("The average price of the %d products is %.2f.\n", n, average);
return 0;
}
I'm pretty sure it is because of this line
n = count-1
Can you explain why you are subtracting 1? In the case where you input 10 numbers, count will equal 10. After subtracting 1 you will then only iterate thru the first 9 indexes in the array.
If you need to subtract 1 (to account for the user inputting a 0 or -1), then change the condition in the last for loop to be <= instead.
for(i = 0; i<=n; i++)
There are two things has to be considered in this program.
1 . n = count-1;
for(i = 0; i<n; i++)
The count variable contains the number of element in the array, while n has been assigned to count - 1 to access from the 0th position , but while iterating the loop condition `i<n` make the loop to run n-1 time (i.e 9 times in this case).
So that the sum calculation failed to calculate the last array element.
average = sum/a;
The variable average and sum are float , whereas n is an int, so the type conversion has to be made while calculating the average.
average = sum/(float)a;
NOTE : a should be replaced by count , which hold the exact count of the element in array.
The complete corrected code is,
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
count++;
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
}
n = count-1;
float sum = 0;
float average;
for(i = 0; i<=n; i++)
{
sum = sum + numbers[i];
}
average = sum/(float)count;
printf("The average price of the %d products is %.2f.\n", count, average);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
When I run the code it gives me a read access error. The problem is with the mean() function I created but I don't know what's wrong. Help?
When I run the code it gives me a read access error. The problem is with the mean() function
Although the mean() function produces read access error, the actual problem is here:
printf("%f", mean(i-1, arr[i]));
You are not passing an array to your function, but its element (it is one past the end of what was written, too, so even the value that you pass is undefined).
You need to pass i for the length, because your mean() treats it as an exclusive upper limit, and you also need to pass arr for the array:
printf("%f", mean(i, arr));
Indexing problem when reading the data also needs to be fixed - you need to remove + 1:
scanf_s("%d", &arr[i]);
// ^
Try this:
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { 0 };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf("%d", &n);
getchar();
while (n > 100 || n < 0)
{
printf("Amount of numbers should be more than 0 and less than 100\n");
scanf("%d", &n);
getchar();
}
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
getchar();
}
printf("%lf", mean(n, arr));
getchar();
}
Your call to the mean function was wrong. You have to pass the entire array, not just one element. Change arr[i] to arr.
Other minor modifications are what I did to make it run on my system. If it works for you otherwise, then great.