I've already asked on here in another post, but none of the answers helped my program. I have a program that asks the user for numbers and calculates the mean, median, and mode. Then the program should prompt the user to play again, and if the user selects y or Y it should replay the game, n or N to stop, and if something else other than that, say invalid and please type y or n to bla bla you get it. here is my main, and my method goAgain() :
#define MAX 25
#include<stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int readTotalNums();
void fillArray(int total, int nums[]);
void sortArray(int nums[], int total);
double findMean(int nums[], int total);
double findMedian(int nums[], int total);
int findMode(int nums[], int total);
void printResults(double mean, double median, double mode);
bool goAgain();
int main() {
int nums[MAX];
int total;
double mean, median, mode;
bool b;
do {
total = readTotalNums();
fillArray(total, nums);
sortArray(nums, total);
mean = findMean(nums, total);
median = findMedian(nums, total);
mode = findMode(nums, total);
printResults(mean, median, mode);
b = goAgain();
} while (b==true);
return 0;
}
//other methods here
bool goAgain() {
char *temp;
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') {
printf("\nI am sorry that is invalid -- try again");
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
}
if (temp == 'y' || temp == 'Y') {
return true;
} else {
return false;
}
}
every time I play the game, and it gets to the prompt, anything I type it just does nothing, and keeps saying invalid try again, even if the input is a y or N. thanks for your help :)
char *temp; should be char temp;
Don't declare temp as a pointer, also you have not allocated memory for it.
Instead change your declaration to
char temp;
since you already have the right answer, no need to say that again, so I'll add a small tip.
//this
if (temp == 'y' || temp == 'Y') {
return true;
} else {
return false;
}
//is the same as this
return temp == 'y' || temp == 'Y';
//or more generally
if(condition)
return true
else
return false
//is just
return condition
Related
I want to use a function to scanf up to 10 values for an array with the size 10, and also keep track of the number of values that are in the array because I'll need it later for solving some maths about the array, (max value, min value, etc.).
#include <stdio.h>
int enter(int MeasurmentData[], int nrOfmeasurments)
{
for(int i=0;i<10;++i)
{
int MeasurmentData[10];
scanf("%d",&MeasurmentData[i]);
int nrOfmeasurments = 0;
nrOfmeasurments ++;
return nrOfmeasurments;
}
int main()
{
int MeasurmentData[10];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n");
printf("v (View)\n");
printf("e (Enter)\n");
printf("c (Compute)\n");
printf("r (Reset)\n");
printf("q (Quit)\n");
printf("enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') \\ enter values
{
int MeasurmentData[10];
int nrOfmeasurments;
enter(MeasurmentData, nrOfmeasurments);
}
else if(menuoption == 'v') \\\ view values
{
//printf("%d", MeasurmentData[]);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
return 0;
}
}
}
When I run the program it should print Measurment tool 2.0, after the the user has the choice of inputting e(enter) which will scan in up to 10 values into an array, if the user clicks q(quit) while in the enter option already he will be returned to the main menu where he can do whatever.
V(view) prints out the array for the user so that he can view what elements are inside.
C(compute) uses the elements inside and the nr of elements to calculate the highest value element, lowest.
There are some errors in your code. Ill try to explain. You have over declared your variables too many times. And since you have a fixed loop you don't need to count the measurements you will always read 10 measurements.
Below are the code with some modifications. Feel free to ask anything about it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXIMUM_MEASURMENT 10
int enter(int MeasurmentData[])
{
char input[100];
int nrMeasurement = 0;
// reseting Measurment data
for(int i=0;i<MAXIMUM_MEASURMENT;++i) MeasurmentData[i] = 0;
for(int i=0;i<MAXIMUM_MEASURMENT;++i)
{
scanf("%99s", input);
if(strcmp(input, "q") == 0) {
break;
}
MeasurmentData[i] = (int) strtol(input, (char **)NULL, 10);
nrMeasurement++;
}
return nrMeasurement;
}
void showMeasurments(int* MeasurmentData, int length) {
int i = 0;
printf(" ======== Measurment ======== \n");
for(i = 0; i < length; i++) {
printf("%d ", MeasurmentData[i]);
}
printf("\n");
}
int main()
{
int MeasurmentData[MAXIMUM_MEASURMENT];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n" "v (View)\n" "e (Enter)\n" "c (Compute)\n" "r (Reset)\n" "q (Quit)\n enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') // enter values
{
enter(MeasurmentData);
}
else if(menuoption == 'v') // view values
{
// show
showMeasurments(MeasurmentData, MAXIMUM_MEASURMENT);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
break;
}
}
return 0;
}
Edit: i have updated the code. So i have read the comments of your question and there you have explained a little better what you are trying to accomplish. So since you have the requirement to press 'q' to stop reading values. I have to read all measurments as string and convert to integer if it is not the character q.
Edit 2: Thanks to #user3629249 to point out some of the flaws from the code ill update with his suggestions.
I can't seem to figure out what is wrong in my code, when It comes to the first scanf in do while loop, I enter a number and it just stops there.
If I put printf("Something"); after that scanf, it isn't printed so it isn't a infinite loop. Also program does not just freeze, when I go into Task Manager, it takes up CPU, so it is doing something.
My program is supposed to load resistors and calculate their parallel equivalent, until I type in 'done', and then print out the calculated number.
#include <stdio.h>
#include <stdlib.h>
typedef struct o{char om[20];}RESISTOR;
int finish(char *s)
{
if( s[0]=='d' && s[1]=='o' && s[2]=='n' && s[3]=='e' && s[4]==0 )
return 0;
else return 1;
}
int power(int n, int pows)
{
int expo=1;
while (pows)
{
expo*=n;
pows--;
}
return expo;
}
int convert(char *s)
{
int broj,c;for(c=-1;s[c];c++);
for(int i=0;s[i];i++)
{broj+=(s[i]-0x30)*power(10,c-i);}
return broj;
}
double paralel(double old, int new)
{
double num;
num=((double)new*old)/(old+(double)new);
return num;
}
int main()
{
int n=0;double para;
RESISTOR *p=(RESISTOR *)malloc(1*sizeof(RESISTOR));
int *convertnum=(int *)malloc(1*sizeof(int));
do
{
printf("R%d= ",n+1);
scanf(" %s", (p+n)->om);
convertnum[n]=convert((*(p+n)).om);
if(n==0) para=convertnum[n];
else if (finish((*(p+n)).om)) para=paralel(para,convertnum[n]);
n++;
if(finish((*(p+n-1)).om))
{
p=(RESISTOR *)realloc(p, n*sizeof(RESISTOR));
convertnum=(int *)realloc(convertnum, n*sizeof(int));
}
}while(finish((*(p+n-1)).om));
printf("\n");printf("\n");
printf("Re= %.2f",para);
free(p);free(convertnum);
printf("\n");
return 0;
}
for(c=-1;s[c];c++); accesses s[-1], which might be 0, which means c-i in power(i,c-i) is negative, and thus the while(pows){pows--} has to go through 2^64 loops which may take a while. Your program should not take any measurable cpu time.
I am trying to submit an answer given on codechef "https://www.codechef.com/problems/FLOW009"
this is my code.
#include <stdio.h>
int main()
{
int t, i;
scanf("%d",&t);
i=t;
float qty[t], per[t], sum[t];
do{
scanf("%f %f", &qty[t-1], &per[t-1]);
sum[t-1] = qty[t-1] * per[t-1];
if(qty[t-1] > 1000 )
{
sum[t-1] = sum[t-1] - (sum[t-1]*10)/100;
}
}while(--t);
do{
printf("%f\n", sum[i-1]);
}while(--i);
return 0;
}
It tells me again again that my solution is wrong and I am unable to understand it.I can't ask questions there as I have less karma.
Two things:
Never store values for each test cases until told as sometimes the test cases may be large ,in place calculate and print output for each test cases. This may save memory ,time and confusion.
What could be done with few lines why to do with too much code and mess.
See this:
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int p,q;
scanf("%d %d",&q,&p);
float ans=(float)q*p;
if(q>1000)
ans-=(ans/10);
printf("%f\n",ans);
}
return 0;
}
You cannot scan an int as a string. The sscanf() funtion scans a string according to the specified format string. This code could use more error checking.
$ cat FLOW009.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv)
{
char aline[100];
int t;
int quantity;
int price;
FILE *fh = fopen("FLOW009.txt", "rt");
if (fh == NULL) {printf("open failed"); exit(1);}
if (fgets(aline, 80, fh) != NULL) {
sscanf(aline, "%d", &t);
if (t < 1 || t > 1000) {
printf("ERROR: Value of t, %d, is outside the range 1 to 1000\n", t);
exit(1);
}
while (fgets(aline, sizeof(aline), fh) != NULL) {
sscanf(aline, "%d %d", &quantity, &price);
if (quantity > 0 && price <= 100000) {
printf("%.6f\n", (float)quantity * (float)price);
} else {
printf("ERROR: Quantity and price are not within accepted limits.\n");
}
}
}
exit(0);
}
I am currently working on a project for school in which I need to program a calculator to determine the mode of a set of numbers. The parameters are the numbers have to be between 1 and 30. Have to check whether the user inserts a number within that range and that the number must be validated as an integer. I have most of it done except my main issues are the for loop in inputing the numbers and validating them and making sure my mode function works. Any suggestions in fixing the issue with the loop? Also I must use a mode function in order to calculate the mode does the one I'm using work well or is there a better way in going about it?
#include <stdio.h>
#include <string.h>
#include <math.h>
int mode(int *num, int size);
int main(int n, char **p) {
int modearray[], size, i;
printf("What is the size of the Array?");
scanf("%d", &size);
for (i=0; i<modearray[size]; i++) {
printf("Enter an integer value (1 to 30): ");
scanf("%d", modearray[i]);
if (modearray[i] < 1 || modearray[i] > 30) {
printf("Please enter a value within the range");
scanf("%d", modearray[i])
}
else if (sscanf(p[i], "%i", &a[i]) != 1) {
printf("ERROR\n");
return -1;
}
}
}
//used the mode function code frome http://www.dreamincode.net/forums/topic/43713- pointers-and-modefunction/
int mode(int *num, int size) {
int currentnum = (*num);
int count = 0;
int modenum = -1;
int modecount = 1;
for (int x=0; x<size; x++) {
if (currentnum==(*num + x)) count ++;
else {
if(count > modecount) {
modenum = currentnum;
// modecount = count;
x--;
}
currentnum=*(num + x);
count = 0;
}
}
}
As Charlie and user2533527 have already indicated, there are errors in the OP code, and they have offered suggestions regarding those errors. There are a few others that I have noted in my edit of your original code below, that without addressing, the code did not build and/or run. So, if you are interested, look at the inline comments at the bottom of this post to see some corrections to your original code.
This answer is focused on validation of input, per your stated objective ( Have to check whether the user inserts a number within that range and that the number must be validated as an integer ) Specifically it appears you need to verify that the numbers input fall within a range, AND that they all be an integers.
If you move all of the validation steps into one function, such as:
int ValidateInput(char *num)
{
if(strstr(num, ".")!=NULL) return FLOAT;
if (atoi(num) < 1) return SMALL;
if (atoi(num) > 30) return LARGE;
return VALID;
}
then the main user input loop can be easily executed to include specific errors, if any, or continue with data collection by using a switch() statement, such as:
status = ValidateInput(number);
switch(status) {
case VALID:
modearray[i] = atoi(number);
printf("Enter an integer value %d: (1 to 30): ", i+2);
break;
case FLOAT:
printf("float detected, enter an integer");
i--;//try again
break;
case SMALL:
printf("value too small, enter value from 1 to 30");
i--;//try again
break;
case LARGE:
printf("value too large, enter value from 1 to 30");
i--;//try again
break;
default:
//do something else here
break;
}
Altogether, this approach does not use the mode function, rather replaces it with ValidateInput() which ensures only numbers that are integers, and within the stated range are included in the modearray varible.
EDIT to include searching for mode (highest occurring number within group)
My approach will do three things to get mode
sort the array,
walk through the sorted array tracking count of the matches along the way.
keep the highest string of matches.
To do this, I will use qsort() and looping in the mode() function.
int mode(int *num, int size) {
int count = 0;
int countKeep=0;
int modenum = -1;
qsort(num, size, sizeof(int), cmpfunc);
//now we have size in ascending order, get count of most occuring
for (int x=1; x<size; x++)
{
if(num[x-1] == num[x])
{
count++;
if(count > countKeep)
{
countKeep = count;
modenum=num[x];
}
else
{
count = 0;
}
}
}
return modenum;
}
Here is the complete code for my approach: (This code will capture the mode of a string of numbers with only one mode. You can modify the looping to determine if the string is multi-modal, or having two equally occuring numbers)
#include <ansi_c.h> //malloc
//#include <stdio.h>//I did not need these others, you might
//#include <string.h>
//#include <math.h>
int ValidateInput(char *num);
int mode(int *num, int size);
int cmpfunc (const void * a, const void * b);
enum {
VALID,
FLOAT,
SMALL,
LARGE
};
int main(int n, char **p)
{
int *modearray, size, i;
int *a;
char number[10];
int status=-1;
int modeOfArray;
printf("What is the size of the Array?");
scanf("%d", &size);
modearray = malloc(size*sizeof(int));
a = malloc(size);
printf("Enter an integer value 1: (1 to 30): ");
for (i=0; i<size; i++)
{
scanf("%s", number);
//Validate Number:
status = ValidateInput(number);
switch(status) {
case VALID:
modearray[i] = atoi(number);
printf("Enter an integer value %d: (1 to 30): ", i+2);
break;
case FLOAT:
printf("float detected, enter an integer");
i--;//try again
break;
case SMALL:
printf("value too small, enter value from 1 to 30");
i--;//try again
break;
case LARGE:
printf("value too large, enter value from 1 to 30");
i--;//try again
break;
default:
//do something else here
break;
}
}
modeOfArray = mode(modearray, size);
getchar();//to view printf before execution exits
}
int ValidateInput(char *num)
{
if(strstr(num, ".")!=NULL) return FLOAT;
if (atoi(num) < 1) return SMALL;
if (atoi(num) > 30) return LARGE;
return VALID;
}
int mode(int *num, int size) {
int count = 0;
int countKeep=0;
int modenum = -1;
qsort(num, size, sizeof(int), cmpfunc);
//now we have size in ascending order, get count of most occuring
for (int x=1; x<size; x++)
{
if(num[x-1] == num[x])
{
count++;
if(count > countKeep)
{
countKeep = count;
modenum=num[x];
}
else
{
count = 0;
}
}
}
return modenum;
}
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
Assuming that the question is about crashing after the scanf in to array:
int main(int n, char **p) {
int *modearray, size, i;
printf("What is the size of the Array?");
scanf("%d", &size);
modearray = malloc(size * sizeof(int)); //imo size of int is 4 so u can replace with
for (i=0; i<modearray[size]; i++) {
printf("Enter an integer value (1 to 30): ");
scanf("%d", modearray[i]);
if (modearray[i] < 1 || modearray[i] > 30) {
printf("Please enter a value within the range");
scanf("%d", &modearray[i])
}
else if (sscanf(p[i], "%i", &a[i]) != 1) {
printf("ERROR\n");
return -1;
}
}
}
I'm having an issue with my menu. I get a number from the user but whenever I get a number it just does option one no matter what I do. What am I doing wrong?
int main()
{
int array[SIZE];
int size = readNum();
fillArray(array, size);
char option = 'y';
do
{
int num = menu();
if(num == 1)
fillArray(array, size);
else if(num == 2)
{
int newSize = readNum();
fillArray(array, newSize);
}
else
{
sortArray(array);
}
}while(option == 'y');
return 0;
}//end main
int menu()
{
printf("1)Change the values of the array\n2)Change the size of the array and the values in the array\n3)Find and display the mean and median\nChoice: ");
int menuChoice = scanf("%i", &menuChoice);
return menuChoice;
}
The scanf function returns the number or successful conversion it made, not the actual converted value. It seems it always succeeding in reading you value, so will return 1 for one successful conversion.
To return the actual user choice, do not assign to it from the call:
int menuChoice:
scanf("%i", &menuChoice);
return menuChoice;
int menuChoice = scanf("%i", &menuChoice);
scanf returns the number of successful conversions, so if the scan succeeds, you are overwriting the converted value with 1.