Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
OK, I edited my program ***I am sorry if this is not formatted correctly (I don't know how to do that to make it easier to read for everyone :( I went through and fixed everything I could, but its still giving me errors.
#include <stdio.h>
#include <stdlib.h>
#define Max 1
#define Min 0
int getValidInteger(char promptString[], char errorString[], int lowerBound,
int upperBound);
float averageIntegerArray(int height[], int count);
float printIntegerArray(char heading[], int values[], int count);
int findExtremeInIntegerArray(int minOrMax, int values[], int count);
//Get user input
int main(int argc, char** argv) {
//Get the number of scores
int n = getValidInteger("Welcome to the Statisticamator! \n"
"How many heights would you like to enter (1-100)?",
"Number of scores must be 1 or higher\n", 1, 100);
printf("N is %d\n", n);
//Read in information from the user
int height[n];
for (int i = 0; i < n; i++) {
height[i] = getValidInteger("Enter height (0 to 100):",
"Height must be between 0 and 100", 0, 100);
}
//Prints average heights
printf("\n");
printf("Average Height: %.2f inches\n", averageIntegerArray(height, n));
//prints minimum and maximum
int min = findExtremeInIntegerArray(Min, values, n); //ISSUE HERE TOO
printf("Minimum value is: %d\n", min);
int max = findExtremeInIntegerArray(Max, values, n);
printf("Maximum value is: %d\n", max);
return (EXIT_SUCCESS);
}
//Average height in array
float averageIntegerArray(int height[], int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += height[i];
}
return (float) sum / count;
}
//Get integer input from user
int getValidInteger(char promptString[], char errorString[], int lowerBound,
int upperBound) {
int input;
//Prompt user for input
printf(promptString);
scanf("%d", &input);
//Validate input
while (input < lowerBound || input > upperBound) {
printf("errorString");
printf("\n");
printf("promptString");
scanf("%d", &input);
}
return input;
}
//Min and Max values
int findExtremeInIntegerArray(int minOrMax, int values[], int count);
int Extreme = values[0];
for (int i = 0; i < count; i++) //THIS IS WHERE IM HAVING 1 ISSUE
{
if ((values[i] < Extreme && minOrMax == Min) || (values[i] > Extreme && `enter code here`minOrMax == Max)) {
Extreme = values[i];
}
}
return (Extreme);
}
You should add a parameter after the " in the following two statements.
printf("Your minimum number is: %d inches \n");
printf("Your maximum number is: %d inches \n");
If you wanted to print 3 variables you would have three format specifiers.
printf("Your maximum number is: %d inches %d feet %d miles \n", inches, feet, miles);
The format specifier is a place holder. What the warning is telling you is that you have not given it a number to format in that place. %d is hanging out by itself.
Related
I am very new to C and am having some issues with a function I am writing. The assignment is to write a function in which it prompts for height and width parameters to draw a box with. I have the function written and it compiles correctly, but the issue I'm having is that I need to call the function twice and save a width from the first call, and a height from the second. Now, this would be easy if I could use pass-by-reference, but I am not allowed to as the function has to be an int. Here is what I have so far.
//LaxScorupi
//11/21/2021
// C
#include <cstdio>
int GetSize(int min, int max)
{
int range;
while (range < min || range > max)
{
printf("Please enter a value between %d and %d: ", min, max);
scanf("%d", &range);
}
return range;
}
/*
This is where I think I am missing something obvious. Currently, I
have printf in place to
just read the value back to me, but I know my "range" will be saved as
whatever my second call
of GetSize is. I've tried creating variables for height and width, but
am unsure how to take
my return defined as range and store it as two different values.
*/
int main ()
{
int min;
int max;
int range;
range = GetSize(2, 80);
printf("Your width is %d\n", range;
range = GetSize(2, 21);
printf("Your height is %d\n", range);
return 0;
}
Thanks in advance- Lax Scorupi
struct
{
int height;
int width;
}range;
range.width = GetSize(2, 80);
range.height = GetSize(2, 21);
print("Height:%d, Width:%d\n", range.height, range.width);
Basically you can just save them in two different variables and store them in an array so u can use them later.I just added the names and the array into your code down here.
#include<stdio.h>
int GetSize(int min, int max)
{
int range;
while (range < min || range > max)
{
printf("Please enter a value between %d and %d: ", min, max);
scanf("%d", &range);
}
return range;
}
int main ()
{
int min;
int max;
int range1, range2;
range1 = GetSize(2, 80);
printf("Your width is %d\n", range1);
range2 = GetSize(2, 21);
printf("Your height is %d\n", range2);
int a[2] = {range1, range2};
printf("%d %d", a[0], a[1]);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to get the prime number n, and calculate the sum of cubes for that n (1^3 + 2^3 + ... + n^3). So far I can figure out how to get the primes. I just can't figure out how to get that same n to calculate its primes. This is what I have so far:
#include <stdio.h>
int main() {
int n, i, c = 0
printf("Enter any number n: ");
scanf("%d", &n);
for(i=2; i<=n/2; i++){
if(n%i == 0){
c=1;
break;
}
}
if (c==0)
printf(%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
printf("Enter that same number n: ");
scanf("%d", &num);
int num, cube, sum = 0, j=1;
while (j <= num) {
cube = j*j*j;
sum = sum + cube;
j++;
}
printf("sum of cubes of %d is %d\n", num, sum);
return 0:
}
I get an error on the second scanf because it says num is undeclared. What should I do to fix this situation.
Use scanf after declaring int num. Also you have used % in 2nd num. Use
int num, cube, sum = 0, j=1;
scanf("%d", &num);
instead of
scanf("%d", %num);
int num, cube, sum = 0, j=1;
Consider this starting piece
long double sumcubs(int n)
{
int i;
long double ret = 0;
for (i = 1; i <= n; ++i)
ret += (i * i * i);
return ret;
}
Once you have encapsulated this sum of cubes function, it should become easier to organize and write the rest of your code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Write a program takes n number of element from user (where, n is specified by user) and stores data in an array.
Then, display the contents of the array on the screen and calculate and display the largest of the temperatures using function.
I understand the question but every time I tried, I always get errors. Can someone correct my code:
#include <stdio.h>
float maximum (float num[]);
int main ()
{
int i,size;
printf ("Enter total number of elements (1 to 100): \n");
scanf ("%d", &size);
float num[]={size};
for (i=1; i<=size; i++) {
printf ("Enter Number %d: ",i);
scanf ("%f", &num[i]); }
printf("Largest Number: %f", maximum (float num[]);
}
float maximum (float num[])
{
int i,size;
float num[]={size};
float max;
max=num[1];
for (i=2; i<=size; i++) {
if (num[i]>max)
max=num[i]; }
return max;
}
thanks
you need something like this.
#include <stdio.h>
float maximum (float num[], int size);
int main ()
{
int i,size;
printf ("Enter total number of elements (1 to 100): \n");
scanf ("%d", &size);
float num[100];
for (i=0; i<size; i++) {
printf ("Enter Number %d: ",i);
scanf ("%f", &num[i]); }
printf("Largest Number: %f", maximum (num, size);
}
float maximum (float num[], int size)
{
int i;
//float num[]={size};
float max;
max=num[0];
for (i=1; i<size; i++) {
if (num[i] > max)
max=num[i]; }
return max;
}
float num[size]={0};
VLA with size number of elements.
Also scanf ("%f", &num[i-1]); otherwise you were accessing array index out of bound invoking UB.
Also in the function
float maximum (float num[])
{
int i,size;
//float num[]={size};
float max;
max=num[0];
for (i=2; i<=size; i++) {
if (num[i-1]>max)
max=num[i-1];
}
return max;
}
Example code:
#include <stdio.h>
#include <stdlib.h>
float mymaximum (int size, float num[]);
int main (void)
{
int i,size;
printf ("Enter total number of elements (1 to 100): \n");
if( scanf ("%d", &size) != 1){
fprintf(stderr,"Error in input");
exit(1);
}
if( size<1 && size>100){
fprintf(stderr,"Wrong input.");
exit(1);
}
float num[size];
for (i=0; i<size; i++) {
printf ("Enter Number %d: ",i);
if( scanf ("%f", &num[i]) !=1){
fprintf(stderr,"Error in input");
exit(1);
}
}
printf("Largest Number: %f", mymaximum(size,num));
return 0;
}
float mymaximum (int size, float num[])
{
float max=num[0];
for (int i=1; i<size; i++) {
if (num[i]>max)
max=num[i];
}
return max;
}
So what were the problems?
float num[]={size}; This is one element array. And then you try to access all the other indices other than 0. This will be accessing an memory out of the array's bound. You have 1 elelemnt array and unless size = 1 that's Undefined behvaior. Anything can happen. Anything including blowing up the code or sudden working at a time.
What was the solution?
Incorporated some error check. In case the user gives wrong input or the number of elements is more than what you expect it to be, then we are terminating the program. Instead of using 100 element array we have used VLA and created an array having size number of elements.
What happened in mymaximum function?
Earlier you have redeclared an one element array in the method. That's wrong - You passed the array so that you can read it. But you didn't do it. Rather you declared again an array and accessed the positions out of the array bounds. Yes again it is undefined behavior.
disclaimer: I'm new to programming
I'm working on this problem
so far ive written this which takes user inputs and calculates an average based on them
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
}
I'd like the user to enter -1 to indicate that they are done entering data; I can't figure out how to do that. so if possible can someone explain or give me an idea as to how to do it
Thank you!
#include <stdio.h>
int main()
{
int i = 0;
float num[100], sum = 0.0, average;
float x = 0.0;
while(1) {
printf("%d. Enter number: ", i+1);
scanf("%f", &x);
if(x == -1)
break;
num[i] = x;
sum += num[i];
i++;
}
average = sum / i;
printf("\n Average = %.2f", average);
return 0;
}
There is no need for the array num[] if you don't want the data to be used later.
Hope this will help.!!
You just need the average. No need to store all the entered numbers for that.
You just need the number inputs before the -1 stored in a variable, say count which is incremented upon each iteration of the loop and a variable like sum to hold the sum of all numbers entered so far.
In your program, you have not initialised n before using it. n has only garbage whose value in indeterminate.
You don't even need the average variable for that. You can just print out sum/count while printing the average.
Do
int count=0;
float num, sum = 0;
while(scanf("%f", &num)==1 && num!=-1)
{
count++;
sum += num;
}
to stop reading at -1.
There is no need to declare an array to store entered numbers. All you need is to check whether next entered number is equal to -1 and if not then to add it to the sum.
Pay attention to that according to the assignment the user has to enter integer numbers. The average can be calculated as an integer number or as a float number.
The program can look the following way
#include <stdio.h>
int main( void )
{
unsigned int n = 0;
unsigned long long int sum = 0;
printf("Enter a sequence of positive numbers (-1 - exit): ");
for (unsigned int num; scanf("%u", &num) == 1 && num != -1; )
{
++n;
sum += num;
}
if (n)
{
printf("\nAverage = %llu\n", sum / n);
}
else
{
puts("You did not eneter a number. Try next time.");
}
return 0;
}
The program output might look like
Enter a sequence of positive numbers (-1 - exit): 1 2 3 4 5 6 7 8 9 10 -1
Average = 5
If you need to calculate the average as a float number then just declare the variable sum as having the type double and use the corresponding format specifier in the printf statement to output the average.
This question already has answers here:
Check if a value from scanf is a number?
(2 answers)
Closed 9 years ago.
my program adds numbers entered by the user. It runs and works great until a character is entered instead of an integer. Is there a simple way to make sure only integers are entered from the keyboard?
Here is my code.
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, sum = 0, i, TotalOfNumbers;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (i = 1; i <= n; i++)
{
scanf("%d",&TotalOfNumbers);
sum = sum + TotalOfNumbers;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
You need to check the return value of scanf. If the input was a valid number, it will return 1. If the input was not a valid number, it will return something else. Here is your code modified to put the checks in.
#include<stdio.h>
#include <stdlib.h>
int get_number()
{
int num;
int ret;
ret = scanf("%d", &num);
if (ret != 1) {
printf("bad number\n");
exit(EXIT_FAILURE);
}
return num;
}
int main(int argc, char **argv)
{
int n, sum = 0, i, TotalOfNumbers;
printf("Enter the number of integers you want to add\n");
n = get_number();
printf("Enter %d integers\n",n);
for (i = 1; i <= n; i++)
{
TotalOfNumbers = get_number();
sum = sum + TotalOfNumbers;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
Check the ferror state on the input stream
scanf("%d",&TotalOfNumbers);
if(!ferror(stdin)){
sum = sum + TotalOfNumbers;
}
In addition to posted answer, there options not general as posted, but quicker.
First if you want to skip some final set of characters.In following example all letters,! and + will be skiped
int n;
scanf("%*[a-zA-Z!+]%d",&n);
printf("\n%d",n);
for input
weweqewqQQWWW!!!!+++3332
the output is
3332
Next option is to use buffer wich allowed to read everything untill number is met, and then read the number. The disadvantage is that buffer size is limited
char buf[25];
int n;
scanf("%[^0-9]%d",buf,&n);
printf("\n%d",n);
For input
fgfuf#$#^^#^##4565
Output
4565