Calculator program issue in C - c

I'm writing a statistical calculator, with 3 different calculation options. The problem is whenever I choose the 2nd option, it wants to print both the answer from the 1st option and the 2nd option. When I choose the 3 option, it just prints the answer the the 3rd option(the wrong answer, but thats probably a mishap in the formula). Here is the results:
Please Enter a number of inputs
3
Please enter number 1
1
Please enter number 2
2
Please enter number 3
3
Statistical Calculator Menu
(1) Mean
(2) Standard Deviation
(3) Range
(4) Restart/Exit
2
Here is the Mean 2.0Standard Devition is 0.8
Now I thought it might be an issue with how I'm calling each function, but the best I can tell thats not the case. Then I thought it might be a value that I didn't initialize, but it seems as though thats not it either. I just need another pair of eyes to see where I went wrong here.
#include <stdio.h>
#include <conio.h>
#include <math.h>
const int MAX_DATA=8;
void menu(float numbers[], int amount);
float mean(float numbers[],int amount);
float standard_dev(float numbers[], int amount);
float range( float numbers[], int amount);
int main()
{
int i=0, amount=0;
float numbers[MAX_DATA];
printf("Please Enter a number of inputs \n");
scanf("%d", &amount);
if (amount>MAX_DATA)
{
printf("You entered too many numbers");
}
else
{
for (i=0; i<amount; i++)
{
printf("Please enter number %d\n", i+1);
scanf("%f",&numbers[i]);
}
menu(numbers,amount);
}
getch();
return 0;
}
void menu(float numbers[],int amount)
{
int input2=0;
printf("Statistical Calculator Menu");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit\n");
scanf("%d",&input2);
if(input2==1)
{
mean(numbers,amount);
}
if (input2==2)
{
standard_dev(numbers,amount);
}
if (input2==3)
{
range(numbers,amount);
}
}
float mean(float numbers[],int amount)
{
int i;
float sum=0;
float average=0;
for (i=0; i<amount; i++)
{
sum=sum+numbers[i];
}
average=sum/amount;
printf("Here is the Mean %.1f", average);
return average;
}
float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount);
for (i=0; i<amount; i++)
{
dev=numbers[i]-mean2;
sumsqr+=dev*dev;
}
variance=sumsqr/(float)amount;
sdev=sqrt(variance);
printf("Standard Devition is %.1f", sdev);
return sdev;
}
float range(float numbers[],int amount)
{
int i;
float diff=0;
for (i=0; i<=amount; i++)
{
diff=numbers[amount]-numbers[1];
}
printf("%f\n",diff);
return diff;
}

float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount); // Here it is.
It calls the mean function, which actually prints something :) You can add boolean flag shouldPrint to functions and pass it as true when you want to print it.
Also, this problem is easily solvable with simple debugging your code...if actually looking at it doesn't seem to help...

Related

C - Beginner question, calling my print function wrong

Code isn't done, so variables like KWHPrice can be ignored.
When I'm trying to run my code only the first print is displayed correctly, if I enter let's say 3 4 6 2 5, I get 4194432 (address), is suspect it's because I'm referring to int smallest wrong, as it's both a variable in main and in the function, hence two different variables. I would like some guidance
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
int main(void){
int num, smallest;
printf("\nEnter no of elements :");
scanf("%d", &num);
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
printf("\nSmallest Element : %d", smallest);
}
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest){
for (i = 0; i < num; i++)
scanf("%d", &KWHPrice[i]);
smallest = KWHPrice[0];
for (i = 0; i < num; i++) {
if (KWHPrice[i] < smallest) {
smallest = KWHPrice[i];
}
}
}
You are redeclaring GET_LOWEST_PRICE in main. To use it you will need to call it with your specified parameters.
ex: GET_LOWEST_PRICE(10.8, 1, 1.8, 0.2)

Function gives me a wrong answer

I'm new to C. I've been tasked to run a program that calculates the percentage of students that passed an exam,based on N grade inputs.I don't really understand how functions work in though.This is what I came up with
#include <stdio.h>
#define MAX_N 300
main()
{
int N,grade,i;
float success(N)
{
float sum=0.0;
for (i=0;i<N;i++) {
if (grade>=5) {
sum+=1;
}
float success=sum/N;
return(success);
}
}
printf("How many students? ");
scanf("%d",&N);
printf("Enter grades(0-10) of %d students ",N);
for (i=0;i<N;i++){
scanf("%d",&grade);
}
printf("%f percent of students have passed the exam ",success(N);
return(0);
}
It looks like it should work, however I always get the wrong result.It is stuck on displaying 0.2 or 0.25 for any input I give.Can somebody help?
The problem is that in grade only the last entered data is being stored. Make grade as an array so that all data can be stored.
I guess you are taking multiple value for grade and not taking array for it.
grade should be an array and in loop scanf("%d",&grade[i]); should be implement.
grade should be an array of N integers so that each and every value is stored. You also forgot to multiply success by 100 to get the percentage.
I think I fixed the code:
#include <stdio.h>
#define MAX_N 300
float success(int grade[],int N)
{int i;
float sum=0.0;
for (i=0;i<N;i++) {
if (grade[i]>=5) {
sum+=1;
}
}
float success=sum/N;
return(success*100);
}
int main(){
int N, i;
printf("How many students? ");
scanf("%d",&N);
int grade[N];
printf("Enter grades(0-10) of %d students ",N);
for(i=0;i<N;i++){
scanf("%d", &grade[i]);
}
printf("%f percent of students have passed the exam ", success(grade, N));
return(0);
}
I think you should examine the code I wrote. A little bad code. But it can help.
#include <stdio.h>
int students_success(int *);
int main() {
int n;
printf("How many students?\n");
scanf("%d", &n);
printf("Enter grades(0-10) of %d students\n", n);
int grade;
int pass_std = 0;
for(int i = 0; i < n; ++i) {
scanf("%d", &grade);
pass_std = students_success(&grade);
}
printf("%.2f percent of students have passed exam.\n", (double)pass_std / n);
}
int students_success(int *grade) {
static int pass_std = 0;
if(4 < *grade) {
++pass_std;
}
return pass_std;
}

Custom average function with array input does not return value in C

I made a program which accepts input for 30 users (About their age)
and then the array was supposed to be an input in a custom function made by me (avg_age)
However the average printed on screen is 0.0000 (for 30 non-zero values)
That is why I think it does not return anything.
#include <stdio.h>
float avg_age(int age[]);
main()
{
int i=0,age[30]={0},intemp;
do{
printf("Input age for 30 users: ");
scanf("%d",&intemp);
if(intemp>0 && intemp<100)
intemp=age[i];
else i--;
i++;
}while(i<30 || intemp<0 || intemp>100);
printf("\nAverage age of 30 users: %f\n",avg_age(age));
float avg_age(int age[]){
int i,avg=0;
for(i=0;i<30;i++)
avg+=age[i];
avg=(float)avg/30;
return avg;
}
}
Take out the function definition of avg_age out of the main. You declared avg as int but it should be declared float to store float values.
float avg = 0.0;
In main intemp=age[i]; is not storing the inputs to the array age instead assigning 0 each time to intemp. Change it to
age[i] = intemp;
Your modified code: (for 5 users)
#include <stdio.h>
#define N 5
float avg_age(int age[]);
int main(void)
{
int i=0,age[N]={0},intemp;
do{
printf("Input age for %d users: ", N);
scanf("%d",&intemp);
if(intemp>0 && intemp<100)
age[i] = intemp;
else i--;
i++;
}while(i<N || intemp<0 || intemp>100);
printf("\nAverage age of %d users: %f\n",N, avg_age(age));
return 0;
}
float avg_age(int age[]){
int i;
float avg=0.0;
for(i=0;i<N;i++)
avg+=age[i];
avg=(float)avg/N;
return avg;
}
Here is the tested code.
Your avg inside avg_age should be a float
float avg_age(int age[]){
int i; float sum=0.0;
for(i=0;i<30;i++)
sum += age[i];
return sum/30;
}
and of course the above function should be outside of main (preferably before it).
BTW, you should have compiled with all warnings and debugging info (e.g. with gcc -Wall -g). The compiler certainly would have warned you! Also, learn how to use the debugger (e.g. gdb)

Weird output for mean calculator

So i'm writing a statistical calculator program, and the first function I started to write was the mean calculator. My issue is that I'm getting extremely large( and wrong) values for the answers.
Please Enter a number of inputs
4
Please enter number 1
1
Please enter number 2
2
Please enter number 3
3
Please enter number 4
4
Statistical Calculator Menu
(1) Mean
(2) Standard Deviation
(3) Range
(4) Restart/Exit
1
3940705125981218000000000000000000.000000
Here is my source code.
const int MAX_DATA=5;
void menu(float numbers[], int amount);
float mean(float numbers[],int amount);
int main()
{
int i, amount;
float numbers[MAX_DATA];
printf("Please Enter a number of inputs \n");
scanf("%d", &amount);
if (amount>MAX_DATA){
printf("You entered too many numbers");
}else{
for (i=1;i<amount+1;i++){
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
menu(numbers,amount);
}
getch();
return 0;
}
void menu(float numbers[],int amount)
{
int input2;
printf("Statistical Calculator Menu");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit");
scanf("%d",&input2);
if(input2==1){
mean(numbers,amount);
}
}
float mean(float numbers[],int amount)
{
int i;
float sum;
float average;
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
average=sum/amount;
printf("%f", average);
return average;
}
Can someone point out the mistake, or explain why this isn't calculating correctly?
You are not initialising sum so it is taking whatever garbage value was last in that place on the stack. Change:
float sum;
To:
float sum = 0;
Another problem you have is:
for(i = 1; i < amount + 1; i++) {
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
Array indexes start at 0, so this should be:
for(i = 0; i < amount; i++) {
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
Apart from what Mike said,
for (i=1;i<amount+1;i++)
{
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
float mean(float numbers[],int amount)
{
// ..
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
....
From this, you are not filling numbers[0]. But in the mean calculation, using the value at 0 index.
float sum;
float average;
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
sum is not initialized in your program.

I'm trying to fill an Array with user input, I've already prompted the user now I'm trying to write a fillArray function

#include <stdio.h>
#include <stdlib.h>
#define MAX 25
int readTotalNums();
void fillArray (int nums[], int total);
void sortIt (int nums[], int total);
int findMean (int nums[], int total);
int findMedian (int nums[], int total);
int findMode (int nums[], int total);
void printResults (mean, median, mode);
int goAgain ();
int main()
{
int nums[MAX];
int total;
double mean, median, mode;
do
{
total=readTotalNums();
fillArray(total,nums);
sortIt(nums, total);
mean= findMean (nums,total);
mode=findMode(nums,total);
median=findMedian(nums, total);
printResults(mean,mode, median);
}while(goAgain());
return 0;
}
int readTotalNums()
{
int total;
do
{
printf("How many numbers would you like to enter? (1-25)\n");
scanf("%i",&total);
while(getchar()!='\n');
}while (total<1 || total>MAX);
return total;
}
void fillArray (int nums[], int total)
{
int x;
for (x=0; x<total; x++)
{
printf("enter your numbers\n");
scanf("%i", &nums[x]);
while(getchar()!='\n');
}
}
So I decided to just put up what I have already... because maybe the problem comes before my fillArray function...
I keep getting a "This program has stopped working" message, which, I know you usually get if you don't strip your carriage return. I'm very much a newbie, just trying to make it though my only programming class I have to take for my major, so any help would be appreciated!!
You should pass to scanf a pointer to the location it should store the input, not the value that is already there. scanf("%i", &nums[x]);
The second argument to scanf should be a pointer. Here you are passing an integer. Since this is homework I won't give you the exact code, but that is the problem as far as I can tell.
The scanf function expects pointers, not integers. Didn't you get a compiler warning when you compiled? Try this:
scanf("%i", nums + x);
or equivalently
scanf("%i", &nums[x]);

Resources