Function prototyping in C language - c

Why can't my code run when I am using a function name "div". But when I change it to any name like " divi",it runs.Can someone explained it? Im 1st yr.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double div(int, int);
int main(void)
{
int num1, num2;
printf("Enter the numbers: \n");
scanf("%d", &num1);
printf("Enter the numbers: \n");
scanf("%d", &num2);
printf("The answer is %.2f", div(num1,num2));
}
double div(int x, int y)
{
double quot;
quot=((double) x/y);
return quot;
}

The standard library already defines a function called div, so you can't use that name for your own function.

Related

Basic C language task about printing a certain number using %.3f

i just started studying programming at school, and i am so lost already. Here's the task:
Answer all 3 tasks in a separate file and return it.
What is the result if the format string (control character) of the printf function is %.3f and the number to be printed is
456.87654321
0.17023
443.14159
How do i even do this? My code is this but it's obviously wrong.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Give a number 1\n");
scanf("%i", &num1);
printf("Answer is on %.3f", &num1);
return 0;
}
It gave me 0 as answers or 0.000 what ever. Only 0's.
I don't know what to do really, my teacher is already on another subject and has no time helping me much.
This source code:
#include <stdio.h>
int main(void)
{
printf("%.3f\n", 456.87654321);
printf("%.3f\n", 0.17023);
printf("%.3f\n", 443.14159);
}
produces this output:
456.877
0.170
443.142
You declared num1 to be an int (integer... whole numbers only).
Then you read that number from the keyboard.
I'm guessing you're entering 456.87654321.
(hint, that is not a whole number. it will not fit in an int)
Then you try to print it out with:
printf("Answer is on %.3f", &num1);
This has several problems.
%.3f is for printing out doubles, not ints.
You passed the "address" (& sign) of the number you wanted to print. Just pass the variable directly
Fixing up your code, I get:
#include <stdio.h>
int main() {
double num1; // Declare DOUBLE, not INT
printf("Give a number 1\n");
scanf("%f", &num1); // Get a DOUBLE from input, not an INT
printf("Answer is on %.3f", num1); // Remove the & (address)
return 0;
}

Error "too few arguments to function call, at least argument 'format' must be specified" C programming

#include <cs50.h>
//declare functions
int add_two_ints(int a, int b);
int main(void)
{
//ask the user for input
printf("give an integer: ");
int x = get_int();
printf("give me another integer: ");
int y = get_int();
//call function
int z = add_two_ints(x, y);
printf("the result of %i plus %i is %i!\n", x, y, z);
}
//function
int add_two_ints(int a, int b)
{
int sum = a + b;
return sum;
}
when i run the program i get the error too few arguments to function call, at least argument format must be specified
this is a simple function with only two arguments being pass since im new to c programming im trying to figure out where i made the mistake.
whats is the correct way to write function ?
The get_int function included as part of CS50 expects a string for a prompt, which you're not passing. So instead of this:
printf("give an integer: ");
int x = get_int();
You want this:
int x = get_int("give an integer: ");
And similarly for reading y.
I just realize what I was doing wrong the get_int was expecting a string so I just deleted the printf statement and put in on to the get_int so now when it runs it works
#include <stdio.h>
#include <cs50.h>
//declare functions
int add_two_ints(int a, int b);
int main(void)
{
//ask the user for input
int x = get_int("give an integer: ");
int y = get_int("give an integer: ");
int z = add_two_ints(x, y);
printf("the result of %i plus %i is %i!\n", x, y, z);
}
int add_two_ints(int a, int b)
{
int sum = a + b;
return sum;
}

Function isn't working like it should. The execution stops

I am a beginner in c and i was writing this piece of code. This is the first time im using doubles so it might be related.
The code gives the print statement in the main function, then when it enters my function ReadVector() it stops working.
I want to to learn and fix my mistake, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%f",*(x + i));
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
Try this one:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%lf", &x[i]);
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",&m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}

bad exit when trying to find harmonic mean, geo mean, sum, prod, avg using for loop

Can somebody tell me what is wrong with my code?
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
int sum=n1+n2;
float prod=n1*n2;
float hm=nSet/(1/n1+1/n2);
float gm=sqrt(n1+n2);
float avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
originally by initializing in for loop i got 0 as every answer but atleast that printed something. I have to use loops to find the answers and i dont see why for loop would'nt work.
In C if you declare a float and assign a value like 1/2 the result will be 0 because de expression 1/2 is evaluated. If you want to have a result as float put 1.0f/2 or 1/2.0f and will work. In case of having 2 variables and want a float you can do this: (float)n1/n2 or n1/((float)n2) if n1 and n2 are int. Another observation is in the for loop. If you declare your variables again in for they are local in the loop and outside the loop they don't exists.
These code will work:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers. \n");
printf("n1 = ");scanf("%d",&n1);
printf("n2 = ");scanf("%d",&n2);
for(;nSet<2;nSet++)
{
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1.0f/n1+1.0f/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2.0f;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
You are declaring new var in for scope. When the program exit from the loop all of the var created in for scope will be destroyed.
Your code corrected :
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1/n1+1/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
It's work much better now :)
Enter two integers: 10
1
Sum: 11
Product: 10.00
Average: 5.00
Geometric mean: 3.32
Harmonic mean: 1.00

Math functions in C

I am using Code::blocks to compile my first multiple source file, learned from "C Programming in easy steps" by Mike McGrath. Unfortunately my math functions seem to be having issues. Here's the header that contains the functions:
/* this header file contains utility functions */
int square(int x); /* function prototypes */
int multiply(int x, int y);
int square(int x)
{
return (x*x);
}
int multiply(int x, int y)
{
return (x*y);
}
The only function having the problem is "square()". It reads the input of "2" as "2293356" and outputs the square as "553755367"... What the heck?!?
Here's the menu.c file... There's menu.c, ops.c, calc.c, and utils.h. Abaov is the .h.
MENU.c
include
void menu();
void menu()
{
int num;
printf("\n\tEnter the number of an operation:\n");
printf("\t1. Square a number\n");
printf("\t2. Multiply two numbers\n");
printf("\t3. Exit\n");
scanf("%d", &num);
switch(num)
{
case 1 : getnum(); break;
case 2 : getnums(); break;
case 3 : return;
}
}
Here's ops.c...
#include <stdio.h>
#include "utils.h"
void getnum();
void getnums();
void getnum()
{
int num;
printf("Enter an integer to be squared: ");
scanf("%d", &num);
printf("%d squared is %d\n, num, square(num)");
menu();
}
void getnums()
{
int num1, num2;
printf("Enter two numbers to be multiplied, ");
printf("seperated by a space: ");
scanf("%d", &num1);
scanf("%d", &num2);
printf("%dx%d = %d\n", num1, num2, multiply(num1, num2));
menu();
}
This is the last part of the program, calc.c,
#include <stdio.h>
int main()
{
menu();
printf("end\n");
return 0;
}
The square of 2293356 doesn't fit into int and therefore overflows which leads to undefined behavior! As to why it reads 2 as 2293356 cannot be answered without more code.
Update:
And here's your real error:
printf("%d squared is %d\n, num, square(num)");
should be
printf("%d squared is %d\n", num, square(num));
:)

Resources