Hi I'm somewhat very new to programming and I just came across this new error called "too few arguments to function" and have no idea what this means. I've been trying to figure out for a while whats exactly wrong with the code, but I'm having no luck so far. Any advice? Thanks in advance!
int sumRange(int lowerNumber, int higherNumber);
int main()
{
int lowerNumber,
higherNumber,
sum;
scanf("%d%d", lowerNumber, higherNumber);
printf("\nThe smaller number is %d and the larger number is %d", lowerNumber,higherNumber);
printf("\nThe sum number of all ranges for both numbers is: %d", sumRange(sum));
}
int sumRange(int lowerNumber, int higherNumber)
{
int x,
total;
for(x = lowerNumber; x <= higherNumber; x++)
{
total = total + x;
}
return x;
}
You have to initialize the "total".
int sumRange(int lowerNumber, int higherNumber)
{
int x,
total;
total = 0;
for(x = lowerNumber; x <= higherNumber; x++)
{
total = total + x;
}
return total;
}
Function main() lacks initial { and final }.
Also, in function sumRange, you may want to return total, not the x.
Also, in line "printf("\nThe sum number of all ranges for both numbers is: %d", sumRange(sum));"
you are calling sumRange with only one parameter. You should call it with two parameters: sumRange(lowerNumber,higherNumber)
I just came across this new error called "too few arguments to function" and have no idea what this means
It means that the function wasn't supplied all of the arguments that are present in its definition.
The function was defined here:
int sumRange(int lowerNumber, int higherNumber);
As you can see, there are two arguments in the definition: int lowerNumber, int higherNumber. But your use of that function here:
printf("\nThe sum number of all ranges for both numbers is: %d", sumRange(sum));
Only supplies that function with one argument. Since you are reading the arguments in with scanf, the correct use would be:
printf("\nThe sum number of all ranges for both numbers is: %d", sumRange(lowerNumber, higherNumber));
Related
This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 4 years ago.
So I'm writing a program about finding the sum number of all ranges for instance, if I enter a 1 and then a 10 it should display 55, but instead it display some long random number. Does anyone have a clue what's wrong with this code? Thanks!
#define <stdio.h>
calculateSum(int lowNumber, int highNumber);
int main()
{
int lowerNumber,
higherNumber;
scanf("%d", lowerNumber);
scanf("%d", higherNumber);
printf("The sum of all ranges is: %d", calculateSum(lowerNumber, higherNumber));
int calculateSum(int lowNumber, int highNumber)
{
int total;
for(int x = lowNumber; x <= highNumber; x++)
{
total = total + x;
}
return total;
}
You're not initializing total before its first use as a rvalue (which is the RHS of assignment total = total + x;) thus its value remains undefined (and indeterminate.)
In addition to #biplls answer there is also another issue:
int lowerNumber,
higherNumber;
scanf("%d", lowerNumber);
scanf("%d", higherNumber);
is UB (produces a warning on compilation and a SEGFAULT during runtime). printf expects an int*, not int. The correct code would be
int lowerNumber,
higherNumber;
scanf("%d", &lowerNumber);
scanf("%d", &higherNumber);
A few errors here
it's #include <stdio.h> not #define
When using scanf you have to add an & before the var you read into (in this case!! )
The main fn doesn't return anything, it should be returning 0 (normally) (apparently I'm wrong on this , main returns 0 implicitly from C99)
Main fn isn't closed before you write the next fn
total should be set to 0
for loop initialization is allowed only in C99
It is a good idea to declare the return type of the prototype
fixing all this
#include <stdio.h>
int calculateSum(int lowNumber, int highNumber);
int main()
{
int lowerNumber,
higherNumber;
scanf("%d", &lowerNumber);
scanf("%d", &higherNumber);
printf("The sum of all ranges is: %d\n", calculateSum(lowerNumber, higherNumber));
return 0;
}
int calculateSum(int lowNumber, int highNumber)
{
int total;
total = 0;
for(int x = lowNumber; x <= highNumber; x++)
{
total = total + x;
}
return total;
}
Running the above
$ ./sum
1
10
The sum of all ranges is: 55
#include <stdio.h>
#include <conio.h>
int getn(int n, int i);
int main()
{
int n, i;
getn(n, i);
getch();
return 0;
}
int getn(int n, int i)
{
int even = 0;
int odd = 1;
int avg;
printf("Enter ten integers: \n");
for (i = 1 ; i <= 10 ; i++)
{
printf("Integer %d: ", i);
scanf("%d", &n);
if ( n % 2 == 0 )
{
even = even + n;
}
else
{
odd = odd * n;
}
}
avg = even / 10;
printf("\n\nAverage of even numbers: %d", avg);
printf("\nProduct of odd numbers: %d", odd);
}
It seems the even calculations worked but when it comes to odd it gives the wrong answer. Please help
Our instructor wants us to use looping or iterations. No arrays. Please help me
First, your C code needs some correction:
at least give the prototype of getn before using it
getn is defined to return an int and doesn't return anything. Either replace int with void or return a value.
Second,
Your code computes the product of ten numbers, if this product is too big, it cannot be store as-is in an int. For example, it works well if you enter ten times number 3, the result is 59049, but if you enter ten times number 23, it will answer 1551643729 which is wrong because 23^10=41426511213649 but that can't be stored in an int. This is known as arithmetic overflow.
Your average is bad, because you sum ints, but the average is (in general) a rational number (average(2,3)=2.5 isn't it ?). So double avg = out/10.0; (means compute a floating division) and printf("Average %f\n",avg); would be better.
So, I have this code which I need to turn into a function:
int main(void) {
int i=0,seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
printf("\nSeed value is:%d\n\n",seed);
srand(seed);
int a[5][5];
int x,y;
printf("Matrix A:\n");
for(x=0;x<5;x++) {
for(y=0;y<5;y++) {
a[x][y] = rand() %51 + (-25);
printf("%d ",a[x][y]); }
printf("\n"); }
printf("\n\n");
So basically, it produces a 2D 5x5 array of random numbers. This works fine, however my next task is applying a function to this code, with the function name of:
void generate_matrices(int a[5][5])
I have tried multiple times, the closest I got to a successful code was:
#include <stdio.h>
#include <stdlib.h>
void generate_matrices(int a[5][5]);
int main(void) {
int a, seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
srand(seed);
printf("\nSeed value is:%d\n\n",seed);
generate_matrices(a);
return 0;
}
void generate_matrices(int a[5][5]) {
int y,z;
printf("Matrix A:\n");
for(y=0;y<5;y++) {
for(z=0;z<5;z++) {
a[y][z] = rand() %51 + (-25); }
printf("%d ",a[y][z]); }
printf("\n");
}
But this returns the error, "expected 'int(*)[5]' but arguement is of type 'int'.
All/any help is muchly appreciated. To be fair on my part, I have done 90% of the code. This is the only bit I need help with so that I can apply this to the rest of my code.
Cheers!
You have declared a as a single integer on this line int a, seed;
When you call the function with generate_matrices(a); you are passing a single integer instead of a pointer to an array.
Change your declaration line to int a[5][5], seed;
generate_matrices(a); will pass a pointer to the first element in your 5 * 5 array, to the function.
You should really print the results in main and not in the function, then you will know that the array has been modified and is available for use in the body of your program.
You have used unconventional placement of braces '}' and this makes it harder to see what belongs in each part of your for loops.
You have the print statements in the wrong places - as a result only part of the matrix is printed.
This is what it should be (just the results - in main):
printf("Matrix\n ");
for (y = 0; y < 5; y++) {
for (z = 0; z < 5; z++) {
printf("%d\t ", a[y][z]);
}
printf("\n");
}
If you use int a[5][5] and call the function with generate_matrices(a);
a function void generate_matrices(int a[5][5]) {...} compiles without error
#include<stdio.h>
#include<stdlib.h>
void modify(int b[5][5]);
int main()
{
srand(4562);
int i,j,arr[5][5];
modify(arr);
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%d ",arr[i][j]=rand() %51 + (-26)); }
}
return 0;
}
void modify(int b[5][5])
{
int i,j;
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
b[i][j]; }
}
}
So this is the closest I have come to completing it. It produces the number of elements I want, also within the range I want. However its not producing the 5x5 grid I need. Where have I gone wrong?
EDIT: I'm not going for neatness at the moment, I just want to get the program working how I want it too and then i'll neaten it up.
EDIT 2: Never mind, realised what I didn't include. Its fine now. Thanks for the help.
I am passing an array of exam grades to the examAverage function along with the size of the array. When I printf the array contents in the for loop they are correct, but when they are added together the sum is wrong. Even if I printf immediately after I set double sum = 0, it says sum = 2. By the time the function is complete I get an extremely large number. Any help is appreciated.
double examAverage(int arr[], int size){
int i;
double exAvg=0;
double sum =0;
printf("Sum = %d\n", sum);
for (i = 0; i < size; ++i)
{
printf("Sum = %d\n", sum);
sum = arr[i];
}
exAvg = sum / size;
return exAvg;
}
Main
double examAverage(int arr[], int size);
printf("How many exam grades would you like to enter?\n");
scanf("%d", &examGrad);
int exams[examGrad];
printf("Enter exam grades \n");
for(int i=0; i<sizeof(exams)/sizeof(exams[0]); i++){
scanf("%d", &exams[i]);
}
double exAvg;
exAvg= examAverage(exams, examGrad);
printf("Exam average = %d", exAvg);
Output
How many exam grades to enter?
2
Enter exam grades
100
50
Sum = 2
Sum = 10
Sum = 1079574528
Exam Average = 1081159680
Don't You mean sum += arr[i]; in the for loop in examAverage?
Also, Your code labeled Main is not really valid at all, first, there is no declaration of examGrad, second the function declaration at the top does whoever knows what, third, sizeofs in the for loop can be replaced by the examGrad, the %d was already mentioned.
If You ask questions about simple code just include all of it, If it is too much, cut out the unnecessary parts or ensure us that they are there, otherwise we don't know if the problem is the missing code or not.
Do not use %d to print a double; that is for integers. Use %f. The different way in which integers and floating point numbers are stored accounts for the problems that you are seeing.
You should have seen a warning about this when you compiled, along the lines of:
format specifies type 'int' but the argument has type 'double'
I'm trying to calculate the size of the file . The process I've followed is to read the file and store it in an array and calculate its size. However,I really don't know ... I tried n number of ways..I've to pass this size as an attribute to the frequency function.along with the name of the array.
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int ??????, int x)
{
int count = 0;
int u;
for (u = 0; u < ??????; u++)
{
if ( theArray[u]==x)
{
count = count + 1 ;
/*printf("\n%d",theArray[u]);*/
}
else
{
count = count ;
}
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int r = 0;
int num;
int theArray[100];
int there[100];
int n;
int g;
int x;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",(integers[i]));
there[r] = integers[i];
i++;
}
//printf("%d",there[r]);
//printf("\n%d",file);
//fclose(file);
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf("\n%d", &x);/*Stores Number To Search For Frequency*/
frequency(integers,????????,x);
getch();
fclose(file);
}
?????? is the size of the integer array from where i read the file and stored it.
I could not find a way to calculate the size of the array into which i copied my file. My idea is to calculate the frequency of a number in that file and calculate the probability of it's occurrence and thereby calculating entropy..Suggestions please!
I don't know why you are initializing so many variables and some of them with awkward names like ??????.
Your main problem is that the call to function should be
frequency(integers, i, x);
Your code with the awkward irrelevant parts removed will look like
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int number, int x)
{
int count = 0;
int u;
for (u = 0; u < number; u++)
{
if ( theArray[u]==x)
count++;
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int num;
int x;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",integers[i]);
i++;
}
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf(" %d", &x);/*Stores Number To Search For Frequency*/
frequency(integers,i,x);
getch();
fclose(file);
}
There are a lot of parts of this code that don't make sense, but I assume it is your debugging trying to figure out what is wrong. The answer to your specific question is:
For each value read from the file you set integers[i] to the value and then increment i. Thus i is the count of items in integers. You then pass integers to frequency(), so i should be passed to the second parameter as the count.
Note that if there are more than 100 values in the file, you will over index integers and cause unpredictable behavior.
To calculate length of array:
int len= sizeof(arr)/sizeof(arr[0]);
It will give length of array without looping.