c language minimum always gets 0 [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 3 months ago.
Improve this question
'min' repeatedly outputs zero instead of the actual smallest number. Not sure where i went wrong on this. Any tips would help.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks,i=0,max,min,sum=0;
float avg;
for(i=0;i<2;i++)
{
printf("iNPUT marks %d:",i+1);
scanf("%d",&marks);
sum=sum+marks;
if (marks>max)
max=marks;
if(marks<min)
min=marks;
}
avg=sum/2;
printf("marks:%.2f\n",avg);
printf("max:%d\n",max);
printf("min:%d\n",min);
return 0;
}

You forgot to initialize min and max.
As a result when you compare marks again those two variables, the values of min and max are garbage, invoking Undefined Behavior.
Change this:
int marks,i=0,max,min,sum=0;
to this:
int marks, i = 0, max = INT_MIN, min = INT_MAX, sum = 0;
Those macros require you to include limits.h library.
Complete example:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int marks, i = 0, max = INT_MIN, min = INT_MAX, sum = 0;
float avg;
for(i=0;i<2;i++)
{
printf("Input marks %d:", i + 1);
scanf("%d",&marks);
sum=sum+marks;
if (marks>max)
max=marks;
if(marks<min)
min=marks;
}
avg=sum/2;
printf("marks:%.2f\n",avg);
printf("max:%d\n",max);
printf("min:%d\n",min);
return 0;
}
Which for input 1 and 2 gives:
Input marks 1:Input marks 2:marks:1.00
max:2
min:1

You are not initializing min and max.
The code invokes Undefined Behavior when you compare mark against both variables (containing garbage).
Also you can change the loop initializing min and max during the first iteration:
for(i=0;i<2;i++)
{
printf("iNPUT marks %d:",i+1);
scanf("%d",&marks);
sum=sum+marks;
if(i==0){ // First iteration both values are the same
max=marks;
min=marks;
}
else{
if(marks>max)
max=marks;
if(marks<min)
min=marks;
}
}

Related

integer print wrong number [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 8 months ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
int k;
printf("Masukkan batas bilangan genap = ");
scanf("%i", &k);
printf("Bilangan genap dari 0 sampai %i adalah :\n");
for (int i=0; i<=k;i+=2){
printf("%i\n", i);
}
}
int main(){
genap_generator();
system("pause");
}
I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error
in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.
The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.
What the function does print right now is what is placed on the stack where k should have been.
Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.
Welcome to our community! Could you use English language in your code next time?
The problem was with printing in printf, you did not declare which variable you wanted to print. Here is the working code:
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator()
{
int k;
printf("Enter an even number limit = ");
scanf("%i", &k);
// Had to declare, which variable to print
printf("The even numbers from 0 to %i are:\n", k);
for (int i = 0; i <= k; i += 2)
{
printf("%i\n", i);
}
}
int main()
{
genap_generator();
system("pause");
}

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

'while' loops for sum of n numbers [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
Im trying to find the sum of n numbers using a while loop so that it runs like so:
How many numbers: 3
-3,
4,
13,
The sum is: 14
However what I am getting is this:
How many numbers: 3
2,
1,
The sum is: 3
I dont understand it, because i set i = 0
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
while (i < numbers) {
scanf("%d", &numbers);
sum = sum + numbers;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
A correct solution would be:
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
int number; // use different variable for the input numbers
while (i < numbers) {
scanf("%d", &number);
sum = sum + number;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
The problem was that you were using one variable for two different things.

My program.exe has stopped working [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 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
int main(void){
int n, i, a[10], sum = 0;
for(i = 0; i < 10; i++){
printf("Enter the marks of %dth student ", i + 1);
scanf("%d", a[i]);
sum = sum + a[i];
}
printf("The total sum is %d", sum);
return 0;
}
Is there an error in my program?
Everytime I run the program, after entering the marks for the first student, I get an error saying that my program has stopped working!
This happens for most of my programs where I have used arrays!
It should be
scanf("%d",&a[i]);
Pass-by-pointer, not by value. Unfortunately, some compilers cannot perform compile-time type safety checks on calls to scanf(). So basically scanf() is treating your (uninitialized value in) a[i] as a pointer, which leads to undefined behavior.
Try this:
#include <stdio.h> //stdio not Stdio
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
scanf("%d",&a[i]); // &a[i] not a[i]
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
scanf needs a pointer, not the value.
You invoked undefined behavior by passing data having the wrong type to scanf(). You have to pass int* to scanf(), not int, for %d.
I also corrected the #includes and added input error check.
Try this:
#include<stdio.h>
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
if(scanf("%d",&a[i])!=1){
fputs("read error\n",stdout);
return 1;
}
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}

C language_Error:expected ')' before ';' token [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 6 years ago.
Improve this question
Just in case you need to know what the program I'm working on -It's a homework question:A five-digit number is entered through the keyboard. Write a function to obtain the reversed number and another function to determine whether the original and reversed numbers are equal or not. Use this functions inside the main() and provide the necessary arguments to get a result.
My code is:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (){
int num,result;
printf("Please enter a number that has five digits:");
scanf("%d", &num);
result=Equality(num);
return 0;
}
int Reversed(int num){
int number=num;
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while ((num>0));
return rev;
}
int Equality(num){
int reve,numb;
if ( (numb=num)== (reve=Reversed(num);) )
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
}
There are some points to fix:
Remove semicolon in if ( (numb=num)== (reve=Reversed(num);) ), which is not ought to be. ( the main problem)
Format your code properly.
Equality() doesn't have return statement, so what it returns is not usable.
I guess Equality() is supposed to only determine that, not to print the result.
The variables reve and numb in Equality(), and number in Reversed() are not used other than being assigned, so you won't need them.
Type of function arguments shouldn't be omitted.
corrected code:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (void){
int num;
printf("Please enter a number that has five digits:");
if(scanf("%d", &num) != 1){
puts("read error");
return 1;
}
if(Equality(num))
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
return 0;
}
int Reversed(int num){
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while (num>0);
return rev;
}
int Equality(int num){
return ( num == Reversed(num) );
}

Resources