C programming about scanf and array [closed] - c

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
I try to make this program when I enter 3 5 2 5 5 5 0
=>
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number is 4
int main()
{
int a[10];
int i,max, count;
printf("Enter numbers: ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
count=1;
for(i=1;i<10;i++)
{
if(max<a[i])
{
count = 1;
max = a[i];
}
else if(max==a[i])
{
count++;
}
}
printf("The largest number is %d\n",max);
printf("The occurrence count of the largest number is %d",count);
return 0;
}
It is my code, but it is totally wrong..
I don't know what should I do
please help me

You've got several errors.
In your first loop, you want to read into a[i], not a[10].
Your code corrently assumes you always type in 10 numbers. It looks like you wanted a value of 0 to end the list. Me, I'd use end-of-file to end the list. To check for 0 you need an extra line if(a[i] == 0) break; in the loop. To check for EOF (or other non-numeric input which will cause problems) you could check to see that scanf returns 1.
In case you enter less than 10 numbers, you'll need a new variable that knows how many variables you actually entered. I set nn = i after the first loop.
Then you just need to change the second loop to run from 1 to nn, not 10.
Putting this all together, we have:
#include <stdio.h>
int main()
{
int a[10];
int i,max, count, nn;
printf("Enter numbers: ");
for(i=0;i<10;i++)
{
if(scanf("%d",&a[i]) != 1) break;
if(a[i] == 0) break;
}
nn = i;
max=a[0];
count=1;
for(i=1;i<nn;i++)
{
if(max<a[i])
{
count = 1;
max = a[i];
}
else if(max==a[i])
{
count++;
}
}
printf("The largest number is %d\n",max);
printf("The occurrence count of the largest number is %d\n",count);
return 0;
}
This seems to work.

Code is OK other than the user needs to enter 10 numbers.
There is no need to store previous numbers, so code can simply examine each new number until '\n' is encountered. Just keep look for the end-of-line before looking for the number.
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
int main(void) {
int count = 0;
int max = INT_MIN;
printf("Enter numbers: ");
for (;;) {
int ch;
int num;
while (isspace(ch = fgetc(stdin)) && ch != '\n') {
;
}
if (ch == '\n' || ch == EOF) break;
ungetc(ch, stdin);
if (scanf("%d", &num) != 1) break;
if (num >= max) {
count++;
if (num > max) {
max = num;
count = 1;
}
}
}
printf("The largest number is %d\n", max);
printf("The occurrence count of the largest number is %d", count);
return 0;
}

Related

a C program that reads a sequence of numbers and display a message [closed]

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 4 years ago.
Improve this question
The program should have the following guidelines:
The numbers are read from the standard input.
The first number is the length of the sequence (n) followed by n numbers.(ie, if you put '54321' the length of the sequence is 5 numbers)
If n is 0 or negative, the program displays the message “Error_1” followed
by a new line on the standard input.
If the length is shorter than n, it displays “Error_2” followed by a new line and quits.
I'm finding point number 2 difficult
My code is:
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else if(){
printF("Error_2\n")
}
}
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else{
scanf("%d",&j);
k=0;
while(j>0)
{
k++;
j=j/10;
}
if(k<i)
printf("Error_2\n");
}
}
so what i did was, i found out the length of number entered and if the length does not match with provided length , it prints error2. i found out the length by continously dividing the number by 10 till it becomes 0.
Check this out. You can use log base 10 to compute the length of the sequence. Easier and clener.
To compile the code use -lm flag.See the following command:
gcc sampleFilename.c -lm
#include <stdio.h>
#include<math.h>
int main() {
int i,num, length;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
}
else{
printf("Enter the number: ");
scanf("%d",&num);
length=(int) log10(num)+1;// compute the length of the number .. read about log10
if (length < i ) //length of the sequence is less than the number 'i'
printf("Error_2\n");
}
}
there is something you have to understand
your code:
"if (i <= 0 ) {
printf("Error_1\n");
} else if () {/* I'm talking about this*/
printF("Error_2\n")
}"
commend: /* its mean else if (its empty!!) you should write condition in the if, what you need to do its just use if and not else or else if
https://www.programiz.com/c-programming/c-if-else-statement*/
currect code below:
#include <stdio.h>
int main()
{
int num,cnt=0;
printf("Enter length: "\n);
scanf("%d", &num);
while(num > 10) //checking first number
{
cnt++;
num /= 10;
}
if(num <= 0)
{
printf("Error_01\n");
}
if(num == (count + 1)!)
{
printf("Error_02");
}
return 0;
}
Try this below code,
Hope this will work
#include <stdio.h>
int main()
{
int i,j,k;
printf("Enter any number: \n");
scanf("%d", &i);
k = 0;
if(i <= 0)
{
printf("Error_01\n");
}
printf("Enter value number: \n");
scanf("%d", &j);
while(j != 0) // check till first digit
{
k++;
j /= 10;
}
if(k != i)
{
printf("Error_02\n");
}
return 0;
}
by the way this was referred from
https://codeforwin.org/2016/10/c-program-to-count-number-of-digits-in-number.html

I have to find two smallest numbers without arrays [closed]

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
So basicly I have this assignement in C, I have to input numbers until I enter 0, and after I enter 0 I have to print 1st and 2nd min number from all that numbers and I can't use arrays. I get that I have to use do-while loop for input but I can't figure out how to find two smallest from all of them. I think that thing can be done with if loops but don't know how to make it as I have only one variable to enter numbers into it (int a). And in input I have error when I enter 0 I'm able to enter one more number before program quits.
#include <stdio.h>
int main() {
int a;
do {
printf("Enter numbers: ");
scanf("%d\n", &a);
//what to do here
}while(a != 0);
You need to add 2 variables to hold the smallest values detected so far. Like
int smallest = INT_MAX;
int second_smallest = INT_MAX;
Then in the loop you need to test if the new input value is smaller than the values stored so far. Something like:
if (a <= smallest)
{
second_smallest = smallest;
smallest = a;
}
else if (a < second_smallest)
{
second_smallest = a;
}
You can use two variables to do what you need
#include <stdio.h>
#include <limits.h>
int main(void)
{
int a = INT_MAX;
int min_1 = INT_MAX;
int min_2 = INT_MAX;
int valid;
do
{
if (a < min_1)
{
min_2 = min_1;
min_1 = a;
}
else if (a < min_2)
{
min_2 = a;
}
printf("Enter numbers: ");
valid = scanf("%d", &a);
}
while ((a != 0) && (valid == 1));
if (valid == 1)
{
printf("Minimum numbers entered are: %d %d\n", min_1, min_2);
}
else
{
fprintf(stderr, "Error in data input\n");
}
}
So:
use limits.h defines to init min variables to the highest value for int type INT_MAX.
for each loop you must test if entered number is a minimum
you must check that user input is valid: check scanf return value.
remove \n in the format string of scanf.

what is wrong with this code? (to check whether a given number is a prime number) [closed]

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 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
void main() {
int a, n, x;
clrscr();
printf("enter a number");
scanf("%d", &a);
n > 1;
a != n && n < a;
if (a / n == x)
printf("a is not a prime no");
else
printf("a is a prime no");
}
If I run this and put a composite number, it still shows it as prime.
your if statement is never true duo to n and x are not initialized. Therefore you only get your else as return. Moreover your expression n>1; and a != n && n < a; return a bool which is not compered to anything. In that case you need to use a for loop.
Here is a link About for loops
int main()
{
int a,n,x = 0;
printf("enter a number: ");
scanf("%d",&a);
for(n=2; n<=a/2; ++n)
{
if(a%n==0)
{
x=1;
break;
}
}
if (x==0)
printf("",n);
else
printf("a is not a prime no");
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();//clearing the screen
int n,x=2,count=0;//Here count is initialised to 0,if it is not prime it remains the same,else it will be equal to 1.You will understand this as you go down
//A number is a prime number if it is not divisible by any other number from 2 and the number before it.
printf("Enter a number : ");
scanf("%d",&n);
while(x<n)//As this checking process should continue till the number just preceding it
{
if(n%x==0)//checking if the number n is divisible by x or not
{
count++;//IF divisible,there is no meaning in continuing,So we are coming out of the loop by incrementing the variable "count"
break;
}
else
x++;
}
if(count==0)
{
printf("%d is a prime number",n);
return 0;//Here if number is prime,There is no need to go further and execute till end,To reduce time complexity ,We will write a return statement to stop executing the code.
}
printf("%d is not a prime number",n);
return 0;
}

Prime Number Check 1-100 (C) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
The code below is to read input from the user to check if an int [1-100] is a prime number or not. (If out of range, will print "Done). If non prime, will output that to the console and the number divisible.
Right now this program is running correctly for 1-10 except for 3 and 9... Any suggestions?
#include <stdio.h>
int main()
{
int num, i;
printf("Number [1-100]:? \n");
scanf("%d", &num);
while(num>0 && num <101)
{
if (num==1||num==2)
printf("Prime\n");
for (i=2; i<=num/2; ++i)
{
if (num%i==0)
{
printf("Non-prime,divisible by %d\n",i);
break;
}
else {
printf("Prime\n");
break;
}
}
printf("Number[1-100]:? \n");
scanf("%d",&num);
}
printf("Done\n");
}
First, make sure your code has appropriate whitespace. This will help you realize when things aren't lined up like you think they are.
#include <stdio.h>
int main()
{
int num, i;
printf("Number [1-100]:? \n");
scanf("%d", &num);
while(num>0 && num <101){
if (num==1||num==2)
printf("Prime\n");
for(i=2; i<=num/2; ++i)
{
if (num%i==0)
{
printf("Non-prime,divisible by %d\n",i);
break;
}
else {
printf("Prime\n");
break;
}
}
printf("Number[1-100]:? \n");
scanf("%d",&num);
}
printf("Done\n");
}
Now you should realize that your else statement happens on the first check! So when 3 is not divisible by 2, it prints "prime."
And then it breaks out of the loop.
And this happens for EVERY number. All your program is doing is checking to see if numbers are divisible by 2.
If you wrote "Odd" instead of "Prime" it would at least be correct there.
This is the kind of problem where setting a flag might be useful (there are other ways to do this, but this is one way).
So you could set a flag, say int isPrime = 1;
Now, if you find out that the number is not prime, you simply set isPrime = 0;.
Finally, at the end of the for loop (let me repeat: AFTER the for loop finishes), you need to check that variable.
And you can say,
if (isPrime == 1)
{
printf("Prime\n");
} else
{
printf("Non-prime.");
}
I'll let you figure out how to print the divisor :)
(For reference, correctly using the flag would look like this -- and for clarity I removed the 'feature' in which it continuously looped)
#include <stdio.h>
int main()
{
int num, i;
int isPrime = 1;
printf("Number [1-100]:? \n");
scanf("%d", &num);
for(i=2; i<=num/2; ++i)
{
if (num%i==0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{
printf("Prime\n");
} else
{
printf("Non-prime.");
}
printf("Done\n");
}
The reason why 3 is behaving differently is that the for logic never reaches 3. For "(i=2; i <= num/2; ++i)", if num equals 3, then the i (being 2) is no longer less than 3/2, which is 1 (after rounding off). So, you should add "num==3" check to the "if (num==1||num==2)".
You're not checking the entire range between 2 and num/2. You need a while loop and a prime flag.
Something like this.
while(num>0 && num <101)
{
unsigned char prime = 1; // set prime flag
i = 2;
while( i < (num/2)+1)
{
if(num%i == 0)
prime = 0;
i++;
}
if(num == 1)
prime = 0;
if(prime == 0)
printf("%d is nonprime\n", num);
else
printf("%d is prime\n", num);
prime = 1;
printf("Number[1-100]:? \n");
scanf("%d",&num);
}

Largest value in array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.
This is what I've come up with:
#include<stdio.h>
int main()
{
const int INPUT = 30 ;
int size [INPUT];
int i, big;
printf("Type integer numbers, followed by q to quit: ");
while (scanf("%d", &size[INPUT]) != 'q')
{
for(i=0;i<size;i++)
scanf("%d",&INPUT[i]);
big = INPUT[0];
for(i=1;i<size;i++)
{
if(big<INPUT[i])
big=INPUT[i];
}
printf("The largest number is %d",big);
return 0;
}
Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.
#include<stdio.h>
int main()
{
const int MAX_INPUT = 30 ;
int input[MAX_INPUT];
int size=0, big;
printf("Type integer numbers, followed by q to quit: ");
while(size < MAX_INPUT){
if(scanf("%d", &input[size]) != 1){
break;
}
++size;
}
if(size ==0){
return 0;
}
big = input[size-1];
while( size-- > 0)
{
if(big<input[size]){
big=input[size];
}
}
printf("The largest number is %d\n",big);
return 0;
}
Tested with GCC 4.1.2 and Linux.
Return value of scanf:
Upon successful completion, these functions return the
number of successfully matched and assigned input items
further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:
const int SIZE = 30 ;
int input[SIZE];
So the while loop should look like:
while (scanf("%d", &input[some_index]) == 1)
and of course this is wrong:
scanf("%d",&INPUT[i]); // should be ==> &input[i]

Resources