error printing value to screen. Max Min values [closed] - c

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 7 years ago.
Improve this question
I am writing an easy code that takes two numbers I enter and tells me which is the Max value and which is the Min value using 2 separate files for functions. here is my code so far. It scans the number and has the correct Max Min but when I go to print the values a crazy number prints.
#include<stdio.h>
int main(void)
{
double num2, max, min, num1;
printf("enter any space-separated pair of decimal numeric values on the same line: ");
scanf("%le%le", &num1, &num2);
max = ComputeMaximum(num1, num2);
min = ComputeMinimum(num1, num2);
printf("%le", max);
printf("%le", min);
return 0;
}
this is my main program, on seperate files in the same project I have:
double ComputeMinimum(double num1, double num2) {
double result;
result = (num1 < num2) ? num1 : num2;
return result;
}
And
double ComputeMaximum(double num1, double num2) {
double result;
result = (num1 > num2) ? num1 : num2;
return result;
}
I would like to have it print to screen:
ComputeMinimum(?, ?) returned ?
ComputeMaximum(?, ?) returned ? (question marks are the values.)

Code is missing prototypes.
That is all, format is OK, functions are OK.
#include<stdio.h>
// Add these to the same file as main()
// Or better yet, add to another file Compute.h and #include "Compute.h"
// here and in the the separate C file
double ComputeMinimum(double num1, double num2);
double ComputeMaximum(double num1, double num2);
int main(void)
...
// suggest adding check
if (2 != scanf("%le%le", &num1, &num2)) {
puts("Input error");
return -1;
}

Related

Error C2371 redefinition; different basic types [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 3 years ago.
Improve this question
I am new to programing and I am trying to learn about functions, so I tried some questions with it to get a better understanding of it.
I am still unsure so right now I got stuck and i don't know how to continue.
I tried to make a first function in the main that has only the answers and that function calls a second function and the second one is calling to a third function
but I dont know how to fix this error i tried changing names but I am not sure what to do and even if i am doing is right
the error I get is related to "void Ex1()" so what can I change?
int main()
{
Ex1();
}
void Ex1()
{
int num1, num2, num3,min;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(min));
}
int function1(int sum)
{
int num1, num2, num3, min, max;
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
The problem you have is that you are trying to call your functions before you have defined (or at least, declared) them. In the C language, when the compiler encounters a call to such an "undeclared" function, it assumes that it is of a type that returns int (it can normally work out what any argument types will be from the actual values given).
So when, in your main function, the compiler comes across Ex1(), it assumes that the function returns an int (even though this isn't actually used). Then, later, when you have your definition for void Ex1(), the compiler has encountered a conflicting (re-)definition - with a different return type.
To fix this, the simplest option is to put a "forward declaration" of any functions used (both Ex1 and function1) before any code that calls them. In your case, this would be:
#include <stdio.h> // This "system header" provides the declarations for "printf" and "scanf"
void Ex1(); // No definition here - just telling the compiler what form it takes.
int function1(int sum); // And the same here
int main()
{
Ex1();
}
// .. . the rest of your code follows
Feel free to ask for further clarification and/or explanation.
You have problem with passing arguments between two functions EX1 and function 1. function1 should have num1, num2 and num3 as arguments.
int main()
{
Ex1();
}
void Ex1()
{
int num1, num2, num3,min;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(num1, num2, num3));
}
int function1(int num1, int num2, int num3)
{
int min, max;
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
This is just the order of definition, try to do it like this:
int num1, num2, num3, min, max;
int function1(int sum)
{
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
void Ex1()
{
int num1, num2, num3;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(min));
}
int main()
{
Ex1();
}

Trying to print a range of Fibonacci numbers in C [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 3 years ago.
Improve this question
The following is an attempt to print a number of fibonacci sequence numbers, determined by the user. Uses a user-define function, fibonacci(int a). It is printing the wrong output, not a recursive sequence, but a continually doubling sequence. How can the code be fixed so that it works correctly?
#include <stdio.h>
int fibonacci(int a);
void main()
{
int number, range;
printf("Enter the number of Fibonacci numbers: ");
scanf("%d", &range);
number = fibonacci(range);
printf("%d\n", number);
}
int fibonacci(int a)
{
int num1 = 1;
int num2 = 1;
int position;
if (a == 1)
{
printf("%d", num1);
}
if (a == 2)
{
printf("%d\n", num1);
printf("%d", num2);
}
if (a > 2)
{
for(position = 1; a >= position; position++)
{
printf("%d\n", num1);
num1 = num2;
num2 = num1 + num2;
}
}
}
This prints the following output for all numbers:
1
1
2
4
8
16
...
The desired output is the fibonnaci sequence (each number is the sum of the two previous ones):
1
1
2
3
5
8
13
...
The problem is that within the loop you first overwrite num1 by assigning num2 to it:
num1 = num2;
Then you make num2 be the sum of itself and num1, but you have just made num1 equal to num2, so this line is effectively just multiplying num2 by two (i.e., adding it to itself):
num2 = num1 + num2;
You need to preserve the old value and use that for the sum, e.g., by adding a third, temporary, variable.
(Apart from this, you also have various other problems, but all of them are such that they should produce compiler warnings. If they don't, enable all warnings and/or get a better compiler. Once you get warnings, don't ignore them but research the cause for each of them and fix all warnings before you even run your code.)

How to stop the int main(void) printf statements when there is an else statement in another function that gives another result? [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 5 years ago.
Improve this question
So everything works in the program so far how the teacher wants it too. However, when I enter 0 for num2 it executes the printf like I want it too, but it also executes the printf at the end of the main. Is there anyway to stop it from also executing the one at end int main when I enter 0? I apologize if my code sucks. This is my first year(semester actually) coding and the teacher doesn't really teach us and I've been learning from the book
#include <stdio.h>
//Program with 3 functions 1) Divides 2) Multiplies 3) Calls the other functions
float multiply(float num1, float num2);
float divide(float num1, float num2);
void math_call(float num1,float num2);
int main(void){
float num1;
float num2;
//printf/scanf allows you to choose values for the functions
printf("Please enter 2 numbers:\n");
scanf("%f", &num1);
scanf("%f", &num2);
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
return(0);
}
//Calls function 1 and 2
void math_call(float num1,float num2){
divide(num1,num2);
multiply(num1, num2);
}
//Divides the arguments and doesn't allow to divide by zero
float divide(float num1, float num2){
if(num2 != 0){
float divide;
divide = num1/num2;
return divide;
}
else{
printf("You can't divide by zero. So your answer for division is undefined.\n");
return(0);
}
}
//multiplies the arguments
float multiply(float num1, float num2){
if(num2 != 0){
float multiply;
multiply = num1*num2;
return multiply;
}
else{
printf("If you multiply by zero you will always get zero.\n");
return(0);}
}
This is the result
Please enter 2 numbers:
4
0
If you multiply by zero you will always get zero.
You can't divide by zero. So your answer for division is undefined.
Division = 0.000000 Multiplication = 0.000000
RUN SUCCESSFUL (total time: 1s)
Instead of this in your code
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
Rewrite it as below
// call your functions here and fill variables with results
float divResult = divide(num1, num2);
float mulResult = multiply(num1, num2);
// check if num2 is not zero
if(num2 != 0)
{
// if it isn't it will print your message
printf("Division = %f Multiplication = %f", divResult, mulResult);
}

Unexpected end-of-file found error in C programming [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
Hi guys i have been check for my code many many times but i still confuse where is this error come from....
This error appear while build solution...
#include<stdio.h>
#include<conio.h>
int add(int a, int b);
int main()
{
int num1, num2, ans;
printf("Please Enter the two numbers :%d %d", num1, num2);
scanf_s("%d %d", &num1, &num2);
ans = add(num1, num2);
return 0;
}
int add(int a, int b)
{
int sum = a + b;
printf("\nSummition is = %d", sum);
}
Unexpected end-of-file found error
int main() {
int num1, num2, ans;
printf("Enter the two numbers : ");
scanf("%d %d", &num1, &num2);
ans= sum(num1, num2);
printf("\nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}

Why is scanf() taking wrong input? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am completely new to C, and was writing a small very simple program. The problem is that the scanf() is taking wrong input:-
#include <stdio.h>
int main(int args, char*argv[])
{
int num1 = scanf("%d",&num1) ;
int num2 =scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
When I give 34 and 23 as input it is outputting:-
Num1 = 1
Num2 = 1
Why is it so?
Don't assign the return value of scanf to num1 and num2. The return value of scanf indicates if the scan is successful, not what you thought it was.
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
This is because scanf returns the number of characters it matches. scanf("%d",&num1) and scanf("%d",&num2) will return 1. You are assigning that number, i.e, 1 to num1 and num2.
Now do it as follows:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1;
int num2;
scanf("%d",&num1) ;
scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
Suggested reading: comp.lang.c FAQ list ยท Question 3.8.
You're assigning the return value of scanf into your values. scanf returns how many characters it matched; you're already passing a reference to the variable you want scanf to store the result in.
Why are you assigning the return value of scanf into your num1 and num2?
My suggestion:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1, num2;
scanf("%d",&num1);
scanf("%d", &num2);
printf("Num1 = %d\n", num1);
printf("Num2 = %d\n", num2);
return 0;
}

Resources