I'm just now learning programming, so this might seem a tiresome question. But when I try to print the value of an integer, it prints "1537" instead. Here is the function, feel free to scrutinize.
void compare(void) {
int num1;
int num2;
int num3;
int num4;
int smallest;
printf("Please enter four integers:\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
num1 = smallest;
if (num2 < smallest)
num2 = smallest;
if (num3 < smallest)
num3 = smallest;
if (num4 < smallest)
num4 = smallest;
printf("%d is the smallest\n", smallest);
}
you need to assign the values to smallest rather than from it.
Change it to
smallest=num1;
if (num2 < smallest)
smallest= num2;
if (num3 < smallest)
smallest=num3;
if (num4 < smallest)
smallest=num4;
~Sometimes its tough to catch the smallest mistakes~ :)
You don't initialize smallest. Actually, you don't write to it at all, anywhere.
Related
I tried to do this:
#include <stdio.h >
int main(void) {
int common = 0;
int num1;
int num2;
int big;
int small;
int i;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
if (num1 <= num2) {
num1 = small;
num2 = big;
}
else{
num1 = big;
num2 = small;
}
for (i = small ; ; i++)
{
}
I don't know what is meant by to 1 do. I'm beginner in algorithms
but I think it means i++ isn't?
The algorithm says to count from small down to 1. So the repetition should be i--, not i++. Use i >= 1 as the repetition condition to keep looping until you get to 1.
for (i = small; i >= 1; i--)
#include<stdio.h>
void Multi(int num1, int num2)
{
int a, b;
if (num1 < num2)
a = num1, b = num2;
else
a = num2, b = num1;
for (a; a > b; a++)
{
for (int i = 1; i < 10; i++)
printf("%d * %d = %d \n", a, i, a * i);
}
};
int main(void)
{
int a, b;
printf("enter two numbers :\n");
scanf_s("%d %d", &a, &b);
Dan(a, b);
return 0;
}
Both of these programs do not execute the For statement. I think there's some problem with the variables.
This for loop (the body of the loop)
for (a; a > b; a++)
is never executed because in the preceding code a is set such a way that it is less than or equal to b. So a > b always evaluates to false.
You need to write either
for ( ; a < b; a++)
or
for ( ; a <= b; a++)
Also it would be more safer to write the call of printf the following way
printf("%d * %d = %lld \n", a, i, ( long long )a * i);
I am trying to write a program that get bunch of numbers, n >= 5
The program asks from the user to enter n non-negative numbers and calculate min,max and the sum of the numbers.
In case the user enter negative numbers, the function asks to enter a positive number.
I have a problem with the first negative number, any clue what's wrong with the following code?
void main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1;
printf("Enter numbers: \n");
scanf("%d", &num1); //here was the error
min = num1;
int sum = num1;
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
while (num1 < 0)
{
{
printf("Enter again number: /n");
scanf("%d", &num1);
}
}
if (num1 > max)
max = num1;
else if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
}
output:
Enter number:
8
Enter numbers:
-8
7
6
9
10
6
7
6
The max number is 10, and the min is -8, and the sum is 43
check if min<0 assign new entered value of num1 to it.
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
if (min < 0)
min = num1;
while (num1 < 0){
//rest of the code}
}
I have a problem with the first negative number, any clue what's wrong with the following code?
Do not read first number with special code, which lacks a negative test. Simplify and read just like other numbers.
Simply start min,max with extremes.
#include <limits.h>
...
int max = INT_MIN;
int min = INT_MAX;
int sum = 0;
printf("Enter numbers: \n");
for (int i = 0; i < x; i++) { // start at 0
int num1;
scanf("%d", &num1);
while (num1 < 0) {
printf("Enter again number: /n");
scanf("%d", &num1);
}
if (num1 > max)
max = num1;
// else if (num1 < min)
if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d",
max, min, sum);
you should put while after taking the input from the user to prevent any negative number also inside while there isn't need to put double cure prates
#include <stdio.h>
int main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1,sum;
printf("Enter numbers:\n");
sum = 0;
min = num1;
for (int i = 1; i<=x; i++)
{
scanf("%d",&num1);
while(num1<0)
{
printf("Enter again number: \n ");
scanf("%d", &num1);
}
if (num1 > max)
{ max = num1;}
if (num1<min)
{ min = num1;}
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
return 0;
}
````````````
The following code uses a user define function to print fibonacci numbers up to the user's choosing. The only problem is that the output includes the first n fibonacci numbers, followed by a zero. (e.g. 1,1,2,3,5,8,0) How can I get rid of the zero?
#include <stdio.h>
int fibonacci(int a);
int main()
{
int j, number;
printf("Enter the number of terms: ");
scanf("%d", &number);
printf("Fibonacci Series: ");
j = fibonacci(number);
printf("%d", j);
}
int fibonacci (int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
return 0;
}
I learned that we are supposed to return something when using functions other than main.
If you learned that, you should look for a better learning source. You could well have written:
void fibonacci(int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
}
Hi once again I have found myself stuck with coding. I have found the correct code but I do not understand why my initial coding does not work. Could anyone explain briefly the problem?
My initial code and the runnable code is posted below.
INITIAL CODE (does not work as expected)
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, num2, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num2);
if (num1 < num2)
{
smallest = num1;
}
if (num2 < num1)
{
smallest = num2;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
RUNNABLE CODE
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num1);
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
In your first attempt you save the value of smallest each loop, but you get rid of it because of the next loop will check always num1 and new entered number in num2.
In the second code, the working one, the loop is keeping track of the last smallest found.
A little thing, the code chunk
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
the second if does not make sense, you can write
if (num1 < smallest)
{
smallest = num1;
}
else
{
smallest = smallest;
}
or better
if (num1 < smallest)
{
smallest = num1;
}