Iteration program - c

I have written the following code:
#include <stdio.h>
/* max_number.c: outputs the largest number of five numbers entered */
void main(void) {
int i, num, max;
for (i = 1; i <= 5; i++) {
printf("Enter a number: ");
scanf("%d", &num);
if (num >= max)
max = num;
}
printf("The maximum number is %d\n", max);
}
When I run the program with any type of data I continually get "The maximum number is 14". Can someone please point me in the direction of what I am doing wrong? Thank you!

Variable max is not initialized.
Try
int i, num, max = INT_MIN;

Related

Can anyone notice where the mistakes is in my code?

Can anyone tell me why is my output wrong "1* 3 = 1" whereas other multiplications is correct? Thanks!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i++;
total = i*n;
}
}
You are printing a value, total, before you assign any value to it. On the first iteration of the while loop, total doesn't contain any defined value.
Here's a way to fix it:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i++;
}
}
Simply calculate total before you print it, not after.

Issue with while loop in C - Rejects input, but then accepts same input second time around

I'm trying to get this program to run properly. It prompts user for a nonzero integer, if a nonzero integer is not entered, the while loop should keep prompting user until a nonzero integer is entered.
Then, it takes the integer and uses that as the amount of double entries the user may enter. The program then calls a function that takes the maximum, minimum, and mean of these user-determined amount of double values.
My issue is that even if I enter a nonzero integer, it rejects it and loops the prompt a second time. But, it accepts the nonzero integer the second time. Code below. Thanks!
#include<stdio.h>
void mmm (int n);
int main()
{
int num;
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
while(num < 1);
{
printf("Invalid number!\n");
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
}
mmm(num);
return 0;
}
void mmm (int n)
{
double min, max, mean, sum, entry;
int i;
printf("***** MIN MAX and MEAN *****\n");
printf("Enter %d numbers separated by spaces: ", n);
sum = 0;
for(i = 1; i <= n ; i++)
{
scanf("%lf", &entry);
sum += entry;
if(i == 1)
{
max = entry;
min = entry;
}
if(max < entry)
max = entry;
if(min > entry)
min = entry;
}
mean = sum/n;
printf("Minimum, maximum, and mean of the %d numbers: %.2lf, %.2lf, %.2lf\n", n, min, max, mean);
}
OK I fixed this by implementing a do-while loop in main:
#include<stdio.h>
void mmm (int n);
int main()
{
int num;
do{
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
}
while (num <= 0);
mmm(num);
return 0;
}

Check the random number by modulo and user's input

im trying to determine how many times the user has guess right the number and place of a random number.
for example, if the number is 1234 and the user's input is 7214
so he guess right only the number 2 and 4.
because of this: 1[2]3[4] == 7[2]1[4].
problem: the program throws me out after i take the user's input. im getting an error which says: "Expression: result_pointer != nullptr"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", num);
saveHits = hit(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}
You're missing a &
scanf("%d", &num);
^

How to find the Highest mark for giving three marks?

#include <stdio.h>
#include<conio.h>
int main ()
{
printf("Enter the Physics ,Chemistry and Maths Marks");
int mark[3]= {40,50,10};
int s[3];
int i;
int sum = 0, highest = 0;
clrscr();
for (i = 0; i < ; i++)
{
sum += mark[i];
if (mark[i] > highest)
highest = mark[i];
}
printf("The Highest Mark is %d: \n", highest);
getch();
return 0;
}
its working fine, I need to give a input dynamically and get the output
How to do that?
Enter the Marks : 30 20 10
output: 30
This:
int mark[3];
mark[3] = scanf("%d",mark[3]);
is wrong because of several reasons. It seems that you don't know how to use scanf properly. It should be
int mark[3];
scanf("%d", &mark[0]);
scanf("%d", &mark[1]);
scanf("%d", &mark[2]); /* Get each number from stdin and store it in the address of the variable given */
or better
int mark[3];
scanf("%d %d %d", &mark[0], &mark[1], &mark[2]);
or even better
int mark[3], i;
for(i = 0; i < 3; i++) /* Loop 3 times */
{
scanf("%d", &mark[i]);
}
You can get the numbers from the input using scanf() and then find the highest number using fmax():
#include <stdio.h>
#include <math.h>
int main()
{
int mark[3] = {0, 0, 0};
int highest = 0;
printf("Enter the Marks\n");
for (int i = 0; i < 3; i++) {
printf("%d: ", i + 1);
scanf("%d", &mark[i]);
highest = fmax(highest, mark[i]);
}
printf("The Highest Mark is %d: \n", highest);
}

Finding min and max between several integers

Seeing the solution to this online on Stackoverflow https://stackoverflow.com/a/19200031/3185410 I tried to come up with another solution by setting max to -infinity and min to +infinity.
The code here by #haccks works pretty fine.
#include <stdio.h>
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("\n%d %d", max, min);
return 0;
}
Here's the one I came up with:
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i;
min=INT_MAX;
max=INT_MIN;
for (i=0;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}
What am I doing wrong with that?
If the users input 5 then 6 you have :
for 5 :
5>-inf => max = 5
for 6 :
6>5 => max = 6
but then min has become 5 and yet it is still INT_MAX.
If you put if instead of else if, you loose in performance but have the right output I think (I cannot test right now).
Set first input as min and max value. And then add logic you wrote. Here you had 2 lines of extra code but you don't have to sacrifice performance.
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i=0;
min=INT_MAX;
max=INT_MIN;
printf("Enter number %d : ",i+1);
scanf("%d",&num);
min=num;
max=num;
for (i=1;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}

Resources