Largest value in array [closed] - c

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]

Related

Counting the amount of digits in a while loop does not work [closed]

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 2 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}

how can I solve the problem without using array as array is complicated to me for now? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Problem is :- Write a C code that ask the user to enter 10 numbers, then ask him to enter another number to search on it in the 10 numbers and print its location in case it is found. In case the number is not found, it will print number no exist.
Code I write is :-
#include<stdio.h>
void main(void)
{
int n, i, value,j;
for (i=1;i<=10;++i)
{
printf("enter number %d : ",i);
scanf("%d",&n);
}
printf("Enter the value to search: ");
scanf("%d",&value);
/*next part of code is not correct
that can not search to find the place of number */
for (j=1;j<=10;++j)
{
if (value == n)
{
printf("value is exist at element number %d",n);
}
else
{
printf("value is not exist\n");
}
}
}
output will be:-
(after enter the numbers).
Enter the value to search is 12.
value is exist at element 9
#include <stdio.h>
int main(){
//there are 10 int in n
int n[10], i, value;
for (i=0;i<10;i=i+1)//array count from 0 ,i=i+1 same as i++
{
printf("enter number %d : ",i);
scanf(" %d",&(n[i]));//& mean get address so you will push what you input to n[0]~n[9]
//little tip before %d remain a space for some reason if you keep learn you will know
}
printf("Enter the value to search: ");
scanf(" %d",&value);//& mean get address so you push what you input to value here
for (i=0;i<10;i=i+1)
{
if (value == n[i])
{
printf("value is exist is element number %d\n",n[i]);
break;//break mean out of for loop
//
}
}
if(i==10){//if search all not found then i will be 10 because after loop i will +1
//if break i will not +1
printf("value is not exist\n");
}
return 0;//remember "int" main() so you need return 0
}
Keep learning you will be stronger
Array is simple

Take user input and continue to use it's variable throughout [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
Not doing anything special. Just taking a user's input between 1 - 500, and then printing the number using for loop for each iteration. It crashes at the for loop. It does not print anything at all.
#include <stdio.h>
int forCounter() {
int num;
int count = 0;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
int i = num;
for (i; count <= i; count++) {
printf(count);
}
getchar();
return 0;
}
I take first input applied to 'num' and check if it's above or below allowed amount with the while loop.
When that's done it leaves and should start the for loop with i taking over for num. I tried using num in the place of i but it didn't work so I tried using a separate variable to see if it'd work.
I get two warnings seen in the image providedTwo warnings
You have to specify the format of output in printf.
int printf(const char *format, ...)
Your code:
#include <stdio.h>
int forCounter(void) {
int num;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
for (int i = 1; i <= num; i++) {
// printf(count); --> Bad
printf("Value = %d\n", i);
}
getchar(); // this will return immediately
return 0;
}

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.

C programming about scanf and array [closed]

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;
}

Resources