Page 1 of 1

Looping problem

Posted: Wed Feb 27, 2013 4:47 am
by Ruhshan
Some of my C codes works correctly. But, when i want to calculate multiple cases so put them inside a loop. They don't give correct output. I tried both in putting directly inside loop or use as a different function for calculation and calling the function inside the loop.
Here is a problem: http://cpbook.subeen.com/2013/02/khoj-search-1.html

to solve, i've written a code:

Code: Select all

#include<stdio.h>
int find(char input[],char test[],long int i,int j);
int main()
{

long int i=0;
int j=0;
int r,tc;
char input[128];
char test[128];

scanf("%d",&tc);

for(r=0;r<tc;r++){
i=0;j=0;
while((input[i]=getchar())!=' '){
    i++;}
while((test[j]=getchar())!='\n'){
    j++;}


find(input,test,i,j);

}

//
return 0;
}
int find(char input[],char test[],long int i,int j)
{int ti,tt,f2,flag,k;
ti=i;
tt=j;

for(f2=0;f2<ti;f2++){

   for(j=f2,k=0,flag=0;j<f2+tt;k++,j++){
   if(test[k]!=input[j])
   flag++;}
   if(flag==0)
   break;

}
   printf("%d\n",f2);

return;
}
If i want to calculate for 3 test cases, for the first case it doesn't give proper output. Actually, if output should 0, it shows 1.Likewise always output is incremented 1 for every first case. But others are okay.

Re: Looping problem

Posted: Wed Feb 27, 2013 9:49 am
by *Mahi*
1. [Most Important]: use proper indentation in your code, use of less spacing makes your code harder to read, and thus makes it less likely to get an answer to your question.

2. The problem with your code was solved with adding

Code: Select all

getchar();
at line 13, after

Code: Select all

scanf("%d",&tc);
The problem that happened there was your code was taking the '\n' character after the number of testcase as a character of the input[] string, so in this test data:

Code: Select all

4
banana ana
banana ban
aquickbrownfoxjumpsoverthelazydog fox
foobar foobar
the input[] string in testcase 1 was "\nbanana" instead of just "banana"

Final code:

Code: Select all

#include<stdio.h>
void find(char input[],char test[],long int i,int j);
int main()
{

long int i=0;
int j=0;
int r,tc;
char input[128];
char test[128];

scanf("%d",&tc);
getchar();
for(r=0;r<tc;r++){
i=0;j=0;
while((input[i]=getchar())!=' '){
    i++;}
while((test[j]=getchar())!='\n'){
    j++;}


find(input,test,i,j);

}

//
return 0;
}
void find(char input[],char test[],long int i,int j)
{int ti,tt,f2,flag,k;
ti=i;
tt=j;

for(f2=0;f2<ti;f2++){

   for(j=f2,k=0,flag=0;j<f2+tt;k++,j++){
   if(test[k]!=input[j])
   flag++;}
   if(flag==0)
   break;

}
   printf("%d\n",f2);

return;
}

Re: Looping problem

Posted: Wed Feb 27, 2013 3:31 pm
by Ruhshan
Thanks a lot. But, when i'm trying to submit the code, its giving a warning "Segment Violation".

Re: Looping problem

Posted: Wed Feb 27, 2013 4:58 pm
by *Mahi*
Declare the input[] and test[] array a little bit over 128, like 135, and try again.

Re: Looping problem

Posted: Wed Feb 27, 2013 7:17 pm
by Ruhshan
Same error message again.

Re: Looping problem

Posted: Tue Mar 12, 2013 1:58 pm
by A.a.m
Funny thing this programming

Re: Looping problem

Posted: Sat Mar 23, 2013 10:47 pm
by A.a.m
আমি এক সাইটে দেখলাম printf এর জায়গায় cout আর scanf এর জায়গায় cin ইউজ করেছে. পার্থক্যটা কী? new comer

Re: Looping problem

Posted: Sun Mar 24, 2013 9:10 am
by Phlembac Adib Hasan
A.a.m wrote:আমি এক সাইটে দেখলাম printf এর জায়গায় cout আর scanf এর জায়গায় cin ইউজ করেছে. পার্থক্যটা কী? new comer
cout() is an object of the iostream in C++. If you are using C++, then use cout(), it works well.
printf() does almost the same things. But it is typically different. It is a formatting function that prints to the standard out. This is mainly used in C.

The same relation also goes with scanf() and cin().

Re: Looping problem

Posted: Sun Mar 24, 2013 11:04 pm
by A.a.m
#include<iostream.h>
main()
{cout<<" thank u ";
}