Looping problem

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

Ruhshan
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Looping problem

Unread post by Ruhshan » Wed Feb 27, 2013 4:47 am

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.

User avatar
*Mahi*
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974
Contact:

Re: Looping problem

Unread post by *Mahi* » Wed Feb 27, 2013 9:49 am

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;
}
Please read Forum Guide and Rules before you post.

Use $L^AT_EX$, It makes our work a lot easier!

Nur Muhammad Shafiullah | Mahi

Ruhshan
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Re: Looping problem

Unread post by Ruhshan » Wed Feb 27, 2013 3:31 pm

Thanks a lot. But, when i'm trying to submit the code, its giving a warning "Segment Violation".

User avatar
*Mahi*
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974
Contact:

Re: Looping problem

Unread post by *Mahi* » Wed Feb 27, 2013 4:58 pm

Declare the input[] and test[] array a little bit over 128, like 135, and try again.
Please read Forum Guide and Rules before you post.

Use $L^AT_EX$, It makes our work a lot easier!

Nur Muhammad Shafiullah | Mahi

Ruhshan
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Re: Looping problem

Unread post by Ruhshan » Wed Feb 27, 2013 7:17 pm

Same error message again.

A.a.m
Posts: 16
Joined: Sun Jan 13, 2013 9:19 pm
Location: chittagong cantonment,chittagongnt

Re: Looping problem

Unread post by A.a.m » Tue Mar 12, 2013 1:58 pm

Funny thing this programming

A.a.m
Posts: 16
Joined: Sun Jan 13, 2013 9:19 pm
Location: chittagong cantonment,chittagongnt

Re: Looping problem

Unread post by A.a.m » Sat Mar 23, 2013 10:47 pm

আমি এক সাইটে দেখলাম printf এর জায়গায় cout আর scanf এর জায়গায় cin ইউজ করেছে. পার্থক্যটা কী? new comer

User avatar
Phlembac Adib Hasan
Posts: 1016
Joined: Tue Nov 22, 2011 7:49 pm
Location: 127.0.0.1
Contact:

Re: Looping problem

Unread post by Phlembac Adib Hasan » Sun Mar 24, 2013 9:10 am

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().

A.a.m
Posts: 16
Joined: Sun Jan 13, 2013 9:19 pm
Location: chittagong cantonment,chittagongnt

Re: Looping problem

Unread post by A.a.m » Sun Mar 24, 2013 11:04 pm

#include<iostream.h>
main()
{cout<<" thank u ";
}

Post Reply