error in following c code [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Note: The question has been edited to make the problem go away.
I have written this code to reverse an array using functions. But there is an error in line 24 saying ' ) expected'. I have read it again and again but i couldn't find the error. Can anybody please reveal it and tell me how to remove it?
#include<stdio.h>
#include<conio.h>
#define max 5
/*function prototype*/
void reverse(int[],int);
void main()
{
int arr[max]={1,2,3,4,5};
int i,j;
clrscr();
printf("the list before reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
reverse(arr,max);
printf("\n the list after reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
getch();
}
/*function for reversing elements of array*/
void reverse(int num[],int max)
{
int i,j,temp;
for(i=0,j=max-1;i<max/2;i++,j--)
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}

max is defined as a macro. So after preprocessing it becomes
void reverse(int num[],int 5)
Which is not valid and you are getting ' ) expected'. If max is constant then there is no need to pass it as a parameter. And also you have a missing for in the function.

You seem to be missing the for keyword in the loop header in reverse().

EDIT:
Ok, ok my first answer was stupid...
(Answered without thinking enough)
Now I got it:
The problem is
#define max 5
Later max (=5) is used as a parameter!

Related

Function to return a specific value in a given input range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...

I sticked in a step that I could not compelet it [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I know the program below is almost wrong and I am seeking for a hep to fix that UP
please help:
#include <stdio.h>
#include <string.h>
int i;
float r;
char c;
char s[48];
int main() {
i=4;
r=3;
c='z';
strcpy(s,"Hi There");
printf("i=%d,r=%d,c=%d")
}
First of all, when you say something is wrong, it might help to point out what it is that is wrong. E.g. add the expected and the actual output, or errors you get when compiling or linking.
Secondly, the error is probably that the printf call prints weird values. It's because you tell it to print three values but you don't actually provide the values to print. Change to
printf("i=%d,r=%d,c=%d", i, r, c);
#include <stdio.h>
#include <string.h>
int main(){
int i;
float r;
char c;
char s[48];
i=4;
r=3.0;
c='z';
strcpy(s,"Hi There");
printf("i=%d, r=%hf, c=\'%c\', s=\"%s\"", i, r, c, s);
return 0;
}

Program should terminate as well find average [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
#include<stdio.h>
int main(){
int a[50],size,i,big,small;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}
I wrote this code in C language which should find the average, longest character and shortest character from the keyboard...Also this program should terminate when the user input asterisk(*)... I cannot figure it out how should I do the average but I did do something to find longest and shortest character.. Please help???
This does look like homework, but to begin with, there's nothing stopping it from returning 0 after the function does the work, so it'll always terminate.
You'll have to put a check for input, to see if a user types in an asterisk, and then return 0, to stay similar to the way you have it.

How to find square root of a number using a recursive function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.

segmentation fault [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
#include <stdio.h>
#include <string.h>
int test(char ch [10],int i,int j )
{
if(i>=j) return 1;
else if (ch[i]!=ch[j]) return 0;
else return (test(ch,i++,j--));
}
int main ()
{
char ch[10];
int m,k;
printf("Donner une chaine de caracteres :\n");
scanf("%s",ch);
k=strlen(ch);
m=test(ch,0,k-1);
if (m==1) printf ("expression palindrome \n");
else printf ("expression non palindrome \n");
return 0;
}
Try replacing this:
else return (test(ch,i++,j--));
...with this:
else return (test(ch,i+1,j-1));
There's no need to assign back into 'i' and 'j' when making that call, since you don't reference them again in the same function invocation. Moreover, i++ evaluates to the original value of i, and not the value of i + 1 (which is what you want here).
So your original code would never actually modify i and j, which would cause it to recurse infitely and cause a stack overflow (so I can't believe people were saying that this wasn't appropriate for SO).

Resources