[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/session.php on line 1042: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4786: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4788: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4789: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4790: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
BdMO Online Forum • View topic - Looping problem

Looping problem

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

Facebook Twitter

Looping problem

Post Number:#1  Unread postby 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.
Ruhshan
 
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Re: Looping problem

Post Number:#2  Unread postby *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
User avatar
*Mahi*
 
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974

Re: Looping problem

Post Number:#3  Unread postby 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".
Ruhshan
 
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Re: Looping problem

Post Number:#4  Unread postby *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
User avatar
*Mahi*
 
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974

Re: Looping problem

Post Number:#5  Unread postby Ruhshan » Wed Feb 27, 2013 7:17 pm

Same error message again.
Ruhshan
 
Posts: 5
Joined: Thu Mar 10, 2011 9:48 am

Re: Looping problem

Post Number:#6  Unread postby 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

Post Number:#7  Unread postby A.a.m » Sat Mar 23, 2013 10:47 pm

আমি এক সাইটে দেখলাম printf এর জায়গায় cout আর scanf এর জায়গায় cin ইউজ করেছে. পার্থক্যটা কী? new comer
A.a.m
 
Posts: 16
Joined: Sun Jan 13, 2013 9:19 pm
Location: chittagong cantonment,chittagongnt

Re: Looping problem

Post Number:#8  Unread postby 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().
User avatar
Phlembac Adib Hasan
 
Posts: 1012
Joined: Tue Nov 22, 2011 7:49 pm
Location: 127.0.0.1

Re: Looping problem

Post Number:#9  Unread postby A.a.m » Sun Mar 24, 2013 11:04 pm

#include<iostream.h>
main()
{cout<<" thank u ";
}
A.a.m
 
Posts: 16
Joined: Sun Jan 13, 2013 9:19 pm
Location: chittagong cantonment,chittagongnt


Share with your friends: Facebook Twitter

  • Similar topics
    Replies
    Views
    Author

Return to Computer Science

Who is online

Users browsing this forum: No registered users and 1 guest