This is problem from textbook. I need to make users be able to enter integers and see the largest and count how many times the largest number appears. I got everything working except for the counting. I have been trying to it figure out for last week.
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
}
numOld = num;
if (numOld == big){
count++;
}else
count--;
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}
There is no need of count--, and you'll have to reset your count value each time you get new value for big.
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
count = 0;
}
numOld = num;
if (numOld == big){
count++;
}
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}
The function could be defined the following way
void bigEof()
{
int num;
int big;
int count = 0;
printf("Please enter an integer: ");
while ( scanf( "%d", &num ) == 1 )
{
if ( count == 0 || big < num )
{
big = num;
count = 1;
}
else if ( big == num )
{
++count;
}
printf( "Please enter next Integer <EOF> to stop: " );
}
if ( count != 0 )
{
printf( "The current biggest number is %d and is repeated %d times\n", big, count );
}
else
{
puts( "You did not enter numbers." );
}
}
Related
Following are the problem, my code and my question:
Do not use an array to hold numbers that user enters in this exercise! Write a program that calculates average of positive numbers that user enters. Program asks user to enter numbers and calculates the average of entered numbers when user enters 0 as the number. The zero is not included in the average. If user enters a negative number the program must print a message telling that only positive numbers are accepted and ignore the negative number.
Here's most of the code written:
#pragma warning (disable:4996)
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", &number);
int count = 0;
while (number != 0)
{
sum = sum + number;
count++;
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
if (count < 0) {
printf("Only positive numbers\n");
}
if (count > 0)
{
printf("AVERAGE = %f", ((float)sum) / count);
}
}
SPECIFIC QUESTION:
If a user enters a negative number, how can I not let it affect the average and prompt the user to enter a positive number?
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", & number);
int count = 0;
while (number != 0) {
if (number > 0) {
sum = sum + number;
count++;
} else {
printf("only positive numbers are accepted\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", & number);
}
if (count > 0) {
printf("AVERAGE = %f", ((float) sum) / count);
}
return 0;
}
You checked if the amount of times the user entered something is negative (which it can never be). Instead just make an if statement before u add the number to the sum.
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", &number);
int count = 0;
while (number != 0)
{
if (number > 0) {
sum = sum + number;
} else {
printf("Only positive numbers\n");
}
count++;
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
if (count > 0)
{
printf("AVERAGE = %f", ((float)sum) / count);
}
}
#include <stdio.h>
int main() {
int number;
int sum = 0;
int count = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", & number);
while (number != 0)
{
if (number > 0)
{
sum = sum + number;
count++;
}
else
{
printf("only positive numbers are accepted\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", & number);
}
if (count > 0)
{
printf("AVERAGE = %f", ((float) sum) / count);
}
if ( count == 0)
{
printf(" no numbers to calculate average.");
}
return 0;
}
The following code is working as desired :-
#include <stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
printf("Please enter the numbers and enter 0 to stop ");
scanf("%d", &number);
while (number != 0)
{
if (number > 0) {
sum += number;
count++; // increase the count only when a positive number is entered
}
else {
printf("Enter only positive numbers\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
printf("AVERAGE = %f", ((float)sum) / count);
}
The problem with Rasumus' code is that it's increasing the count even when a negative number is entered, that will show erroneous average value.
Can anybody tell me where why the correct value of input is not being stored in this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, sum=0;
printf("Enter the number: ");
while(num!=0)
{
sum = sum+num;
scanf("%d", &num);
}
printf("Answerr = %d", sum);
return 0;
}
Here's the output:
Enter the number: 2
0
Sum = 10
Better do:
num= 0;
do
{
scanf("%d", &num);
sum = sum+num;
} while(num!=0);
Note the initialization of num is still needed as scanf could fail which would not affect num.
You cannot know what exactly this part will do :
while (num != 0)
{
sum = sum + num;
scanf("%d", &num);
}
because num is not initialized, so you are adding to sum a value which you do not know. Change it to :
while(num != 0)
{
scanf("%d", &num);
sum = sum + num;
}
so that num has a value when you add it, and also initialize num to something different than 0, for example :
int num = 2;
so that your while loop is executed at least one (in other words, so that you get the chance to read num).
A better approach would be to use a do-while loop like this :
int num = 0;
do
{
scanf("%d", &num);
sum = sum + num;
}while (num != 0);
so as to be sure that your loop will be executed at least once. Even with this approach you should still initialize num in case scanf fails (and therefore num does not get a value).
In order to check the return value of scanf, use this piece of code :
if ( scanf("%d", &num) == 1)
sum = sum + num;
Change your code to:
int main()
{
int num = 0, sum = 0;
printf("Enter the number: ");
do
{
scanf_s("%d", &num);
sum = sum + num;
} while (num != 0);
printf("Answer = %d", sum);
return 0;
}
I replaced the whileloop with a do while one. You've to initialize sum, otherwise you'll work with an undefined value in your first run ( if working with a while loop).
you are adding value of num before reading it
do like this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, sum=0;
printf("Enter the number: ");
do{
scanf("%d", &num);
sum = sum+num;
}
while(num!=0);
printf("Answerr = %d", sum);
return 0;
}
The variable num was not initialized. As result the loop has undefined behavior. Also you should check whether the input was valid before adding values. Take into account that neither declaration from the header <stdlib.h> is used. So you may remove the header.
The program can look the following way
#include <stdio.h>
int main( void )
{
long long int sum = 0;
while ( 1 )
{
int num;
printf( "Enter number (0 - exit): " );
if ( scanf( "%d", &num) != 1 || num == 0 ) break;
sum += num;
}
printf( "\nAnswer = %lld\n", sum );
return 0;
}
Or you could place the prompt before the loop as for example
#include <stdio.h>
int main( void )
{
long long int sum = 0;
printf( "Enter numbers (0 - exit): " );
while ( 1 )
{
int num;
if ( scanf( "%d", &num) != 1 || num == 0 ) break;
sum += num;
}
printf( "\nAnswer = %lld\n", sum );
return 0;
}
And according to the C Standard function main without parameters shall be declared like
int main( void )
I need to make a program that checks to see if an entered value has any repeated digits. The user is asked to enter numbers until the entered value is 0. If there are any repeated digits, it displays "repeated digits" and then asks the user to enter another value. If there are no repeated digits, it displays "no repeated digits" and asks the user to enter another number. So far, this is what i have. It terminates the program when 0 is entered, but it always displays "no repeated digits" even if there are some.
#include <stdbool.h>
#include <stdio.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit;
long int n = 0;
printf("Enter a number: ");
scanf("%ld", &n);
while(n >= 0){
if(n==0)
break;
while (n > 0){
digit = n % 10;
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
n /= 10;
}
if (n > 0)
printf("Repeated digit: %d\n", digit);
else
printf("No repeated digit\n");
scanf("%ld", &n);
}
return 0;
}
A couple of things:
1: A bool only has two states: true and false. If you trying to build a frequency counter of each digit seen, for the presence of a digit more than once, then you should use a data type that can count to at least two, like a char or short or int, or your own enum.
2: This code:
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
Is never going to be evaluated as true since you initialized digit_seen to be false at the start of your main function. What you should be doing is something like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
int digit_seen[10] = {0};
int entry;
int i, flag = 0;
printf("Enter a number: ");
scanf("%ld", &entry);
while(entry > 0)
{
int digit = (entry%10);
digit_seen[digit]++;
if(digit_seen[digit]>=2)
{
printf("Repeated digit: %d\n", digit);
}
entry /= 10;
}
for(i = 0; i < 10; i++)
{
if(digit_seen[i]>1) flag=1;
}
if(!flag)
{
printf("No repeated digits\n");
}
return 0;
}
#include <stdio.h>
int main() {
int seen [10] ={0}; // we set every element for a number is just 0
int N,rem;
printf("Enter the number:");
scanf("%d", &N);
while(N>0){
rem = N%10;
seen[rem]+=1;
N = N/10;
}
int i;
for(i=0;i<10;i++){ // checking the number seen counts
if(seen[i]==0){
continue;
}
printf("%d seen %d times\n",i,seen[i]); // just returned the given numbers informations
}
return 0;
}
I'm new to coding. I'm now learning C programing. I'm having a problem with counting digits. My objective is to only accept a fixed number of digits in the input. For example I want user to input only 7 digits number, so when they input anything else than a 7 digits number, the program should ask them to input again until it gets 7 digits number. Here is my attempt:
int n, count = 0;
printf("Please enter number:");
scanf("%d", &n);
printf("\n");
while (n != 0)
{
n /= 10;
count++;
}
printf("%d", count);
if (count != 7)
{
printf("You can use only 7 digits numbers");
}
You can try this one:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000]; //Enter the maximum number of digits you expect as array size
printf("Please enter number: ");
scanf("%s", s);
while(strlen(s)!=7)
{
printf("%d\n", strlen(s));
printf("You can use only 7 digits numbers\n");
scanf("%s", s);
}
return 0;
}
After taking the input, you can easily convert it to int if you need to.
You can try this by creating a small function to check the number of digits in the number entered for you:
#include <stdio.h>
int count_digit(int n){
int count = 0;
if(n == 0) return 1;
while(n != 0)
{
n /= 10;
++count;
}
return count;
}
int main(void) {
int num;
printf("Please enter number:");
scanf("%d", &num);
while (count_digit(num) != 7){
printf("Please enter only 7digits number:\n ");
scanf("%d", &num);
}
printf("The 7digit number you entered is: %d",num);
return 0;
}
#include <stdio.h>
int main(void) {
double numbersEntered, sum = 0;
do
{
printf("Enter a number: ");
scanf("%lf", &numbersEntered);
sum += numbersEntered;
}
while (/* ??? */);
printf("Sum = %.2lf", sum);
return 0;
}
What should I do in the while statement to stop the loop after the user enters 4 integers?
You need to introduce a counter:
double numbersEntered, sum = 0;
int count = 0;
do
{
printf("Enter a number: ");
scanf("%lf", &numbersEntered);
sum += numbersEntered;
count++;
} while (count < 4);
Make sure you increment it otherwise your loop will never end.
Changing the 4 to a constant (or even a configurable) variable will make the program more flexible, but whether you actually need to do that depends on what your application needs to do.
I can suggest the following solution
#include <stdio.h>
int main( void )
{
const int N = 4;
double sum = 0.0;
for ( int i = 0, success = 1; success && i < N; i++ )
{
double numberEntered;
printf( "Enter a number: " );
if ( success = ( scanf( "%lf", &numberEntered ) == 1 ) ) sum += numberEntered;
}
printf( "\nSum = %.2lf", sum );
return 0;
}
The program output can look like
Enter a number: 1.1
Enter a number: 2.2
Enter a number: 3.3
Enter a number: 4.4
Sum = 11.00
Or the program can look like
#include <stdio.h>
int main( void )
{
const int N = 4;
double sum = 0.0;
printf( "Enter %d numbers\n\n", N );
for ( int i = 0, success = 1; success && i < N; i++ )
{
double numberEntered;
printf( "Enter number %d: ", i + 1 );
if ( success = ( scanf( "%lf", &numberEntered ) == 1 ) ) sum += numberEntered;
}
printf( "\nSum = %.2lf", sum );
return 0;
}
In this case its output might look like
Enter 4 numbers
Enter number 1: 1.1
Enter number 2: 2.2
Enter number 3: 3.3
Enter number 4: 4.4
Sum = 11.00
Instead of using the constant N equal to 4 you can ask the user to enter the number of entered values.
Hey you need to take the number of integers to be scanned as a input.
and then traverse continously.
#include
int main() {
double numbersEntered, sum = 0;
int numbersToBeEntered = 0;
scanf("%d",&numbersToBeEntered);
do
{
printf("Enter a number: ");
scanf("%lf", &numbersEntered);
sum += numbersEntered;
}while (--numbersToBeEntered);
printf("Sum = %.2lf", sum);
return 0;
}
Use a counter to end the loop when the specified number of double you have entered.
Also, remember to check scanf return value, otherwise, if you input something not a double, you will end up adding the old value of numbersEntered, which mostly is not what you want.
#include <stdio.h>
int main(void) {
double numbersEntered, sum = 0;
int cnt = 0;
int ret;
do
{
printf("Enter a number: ");
ret = scanf("%lf", &numbersEntered);
if (ret != 1) continue;
sum += numbersEntered;
cnt++;
} while (cnt < 4);
printf("Sum = %.2lf", sum);
return 0;
}