Friday, June 15, 2018

Hey Could anyone please debug these C codes for me..!? compiled but wrong output!

1.This code is running but only for a single word input

#include<stdio.h>
#include<ctype.h>

int len[100];
int l=0;

int main(){
char s[1000], words[100][50];
int i=0, j=-1, k=-1, l=-1, countw=0, MAX=0,max_index;
printf("Enter a string : ");
gets(s);
i=0;
while(s[i]!='\0'){
if(s[i]==' '){
i++;
}
else if(isalpha(s[i])){
countw++;
j++;
k=0;
while(!isspace(s[i])){
len[l]++;
words[j][k++]=s[i];
i++;
}
words[j][k]='\0';
if(len[l]>MAX){
max_index=j;
}
l++;
}
} 
printf("The longest word is : ");
for(k=0;words[max_index][k]!='\0'; k++){
printf("%c", words[max_index][k]);
}
printf("\nThe number of words in the string is : %d", countw);
return 0;
}


2. This code displays a negative infinity.

#include<stdio.h>

double f(double x){
return (1/(1+x));
}

int main(){
double x0, xn, fx0, fxn, fxi, h, Y=0.0, result=0.0, i, subint;
printf("Lower Bound, Upper Bound, Subintervals : ");
scanf("%lf, %lf, %d", &x0, &xn, &subint);
h=((xn-x0)/subint);
for (i=1.0;i<subint; i++){
fxi=f(x0+(i*h));
Y=Y+fxi;
}
fx0=f(x0);
fxn=f(xn);
h=h/2.0;
result=h*(fx0+2*Y+fxn);
printf("The result is : %lf", result);
}


3. This code too runs but stops in the middle.

#include<stdio.h>

double f(double x){
return (1/(1+x));
}

int main(){
double a, b, h, result, Yodd=0.0, Yeven=0.0;
int i, subintervals;
printf("Enter the lower bound : ");
scanf("%lf", &a);
printf("Enter the upper bound : ");
scanf("%lf", &b);
printf("Enter the number of subintervals ");
scanf("%d", subintervals);
h=((b-a)/subintervals);
for(i=1;i<subintervals; i++){
if(i%2==1){
Yodd=Yodd+f(a+(i*h));
}
else{
Yeven=Yeven+f(a+(i*h));
}
}
result=(h/3)*((f(a))+(4*Yodd)+(2*Yeven)+(f(b)));
printf("\nThe result is : %lf ", result);
return 0;
}



Reply:

1.
 #include<stdio.h>
double f(double x){
 return (1/(1+x));
}
int main(){
 double a, b, h, result, Yodd=0.0, Yeven=0.0;
 int i, subintervals;
 printf("Enter the lower bound : ");
 scanf("%lf", &a);
 printf("Enter the upper bound : ");
 scanf("%lf", &b);
 printf("Enter the number of subintervals ");
 scanf("%d", &subintervals);                                //put Here "&" Before subinterval.. it's working after that...

  h=((b-a)/subintervals);
 for(i=1;i<subintervals; i++){
  if(i%2==1){
   Yodd=Yodd+f(a+(i*h));
  }
  else{
   Yeven=Yeven+f(a+(i*h));
  }
 }
 result=(h/3)*((f(a))+(4*Yodd)+(2*Yeven)+(f(b)));
 printf("\nThe result is : %lf ", result);
 return 0;
}
 
 
Reply 2:
 

Were you able to crack your first code (i.e based on string) ? 
(or) 
Are you still looking for an answer ?

If yes, I could make it work by adding highlighted statements in the code.
##########################################################
          while((!isspace(s[i])) && (s[i] != '\0')){
                len[l]++;
                words[j][k++]=s[i];
                i++;
            }
            words[j][k]='\0';
            if(len[l]>MAX){
            max_index=j;
            MAX = len[l];
########################################################## 

Console output:
Enter a string : handles msg delay active calls
The longest word is : handles
The number of words in the string is : 5
 
 

No comments:

Post a Comment