0

Hi Guys,

Wanted some help with my code.

This should be the OUTPUT.

Enter the size of your array: (array size should be 1-100)
Enter values of your array:
x[0]= (any integer)
x[1]= (any integer but not equal to the value of x[0])
x[2]= (any integer but not equal to the value of either x[0] or x[1])

and so on and so forth

it will then sort the array like the one below:

X[0]=1 x[1]=2 x[2]=3 ......

Enter Number to be Search: (any number either within the array elements or outside of it)

Here is the code below. Everything is working except to the part where it should prompt the user to enter another value for x if the value corresponds to either the values of other elements.

#include <stdio.h>
void main ()
{
	int pass, found, fh, sh, mid, as,x[100],ctr,ns,j,temp;
printf ("\nEnter array size: ");
scanf ("%d",&as);
printf ("\nEnter the values of your array:\n ");

	for (ctr=0;ctr<as;ctr++)
	{
	printf ("\nx[%d]= ", ctr);
	scanf ("%d", &x[ctr]);
				
		}
	for (ctr=0;ctr<as-1;ctr++)
		
	{
	for (j=0;j<as-1;j++)
	{
		if (x[j]>x[j+1])
		{
			temp=x[j];
			x[j]=x[j+1];
			x[j+1]=temp;
		}
	}
	}
printf ("\n\n--Sorted Series--\n\n");
for (ctr=0;ctr<as;ctr++)
{
	printf ("\t x[%d]= %d", ctr,x[ctr]);

}
pass=0;
found=0;
fh=0;
sh=as;
printf ("\nEnter number to be search: ");
scanf ("%d", &ns);
do
{
	mid=(fh+sh)/2;
	if (ns<x[mid])
		sh=mid-1;
	else if (ns>x[mid])
		fh=mid+1;
	else
		found=1;
	pass++;
}
while (fh<=sh &&!found);
if (found)
printf("\nFound at index %d\n ",mid);
	   else
	   printf ("\nNot found\n");
printf("\n\nNumber of passes: %d\n\n ",pass);
}

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

3
Contributors
14
Replies
17
Views
6 Years
Discussion Span
Last Post by protas
Featured Replies
  • declare a int variable flag and you can try something like below code [CODE] for (ctr=0;ctr<as;ctr++) { do{ printf ("\nx[%d]= ", ctr); scanf ("%d", &x[ctr]); if(ctr>0) { for(j=0;j<ctr;j++) { if(x[j]==x[ctr]) { flag=1; printf("\n Number Already Exist!,Enter Another Value\n"); break; } else flag=0; } } else flag=0; }while(flag==1); } [/CODE] Read More

0

>>void main ()

main() always returns an integer -- its never void even if your compiler allows it. Many compilers won't let you declare void main(). Get into the habbit of writing programs correctly and always declare it as int main() or int main(int argc, char* argv[]) I could offer some other suggestions to fix your code but won't because you failed to post the code between code tags.

0

>>void main ()

main() always returns an integer -- its never void even if your compiler allows it. Many compilers won't let you declare void main(). Get into the habbit of writing programs correctly and always declare it as int main() or int main(int argc, char* argv[]) I could offer some other suggestions to fix your code but won't because you failed to post the code between code tags.

I am not really quite following you about posting the code between code tags. Can you please tell me exactly what I need to do?

0

could you please tell me which sorting algorithm you are using in your program..

Edited by vinitmittal2008: n/a

0

>> where it should prompt the user to enter another value for x if the value corresponds to either the values of other elements.

for (ctr=0;ctr<as;ctr++)
{

printf ("\nx[%d]= ", ctr);
scanf ("%d", &x[ctr]);
// here that condition should be checked
}
0

>> where it should prompt the user to enter another value for x if the value corresponds to either the values of other elements.

for (ctr=0;ctr<as;ctr++)
{

printf ("\nx[%d]= ", ctr);
scanf ("%d", &x[ctr]);
// here that condition should be checked
}

Let's say for example the program will prompt me like this:
Assuming that the size of the array is 6

Enter the values for array

x[0]= 1 <<<<== for example I will enter 1
x[1]= 2 <<<<== for exmple I will 2.it will then proceed to prompt me to enter x[2]
x[2]= 2 <<<== however if I enter 2 on x[2], the output should be something like:
"Number already exist.Enter another number."
x[2]= <<<=== the user should enter another number or else it will continue to prompt x[2] until the user can enter numbers different from x[0] and x[1]...


Thanks...

1

declare a int variable flag and you can try something like below code

for (ctr=0;ctr<as;ctr++)
{
  do{
   printf ("\nx[%d]= ", ctr);
   scanf ("%d", &x[ctr]);
   if(ctr>0)
   {
     for(j=0;j<ctr;j++)
     {
       if(x[j]==x[ctr])
        {
          flag=1;
          printf("\n Number Already Exist!,Enter Another Value\n");
          break;
        }
       else flag=0; 
     } 

   }
   else flag=0;
 }while(flag==1);
}

Edited by vinitmittal2008: n/a

0

declare a int variable flag and you can try something like below code

for (ctr=0;ctr<as;ctr++)
{
  do{
   printf ("\nx[%d]= ", ctr);
   scanf ("%d", &x[ctr]);
   if(ctr>0)
   {
     for(j=0;j<ctr;j++)
     {
       if(x[j]==x[ctr])
        {
          flag=1;
          printf("\n Number Already Exist!,Enter Another Value\n");
          break;
        }
       else flag=0; 
     } 

   }
   else flag=0;
 }while(flag==1);
}

thanks for the code. however, it didn't work[IMG]http://img13.imageshack.us/img13/3739/30935451.jpg[/IMG]

0

i watch your output my dear friend.. i think its working.
you have already passed x[0]=3, that is why its not taking same 3 value in x[1], pass different value in x[1] and it will definetely prompt for x[2] and so on...

0

I checked your output file.. its working friend.. just add a printf statement just after flag=1 statement as i edited in my above post.

you have already passes x[0]=3 thats why its not taking same value in x[1], just add this

flag=1;
printf("\n Number Already Exist!,Enter Another Value\n"); // add this 
break;
0

>>I am not really quite following you about posting the code between code tags. Can you please tell me exactly what I need to do?

[code] // <<< this is called a code tag // put all your code here

[/code] // <<< this terminates the code tags

0

I checked your output file.. its working friend.. just add a printf statement just after flag=1 statement as i edited in my above post.

you have already passes x[0]=3 thats why its not taking same value in x[1], just add this

flag=1;
printf("\n Number Already Exist!,Enter Another Value\n"); // add this 
break;

It worked..Thank you so much...

0

Here is the complete code of the program:

#include <stdio.h>
#include <stdlib.h>
void main ()
{
	int as,x[100],ctr,j,flag;
printf ("\nEnter array size: ");
scanf ("%d",&as);
printf ("\nEnter the values of your array:\n ");

for (ctr=0;ctr<as;ctr++)
{
  do{
   printf ("\nx[%d]= ", ctr);
   scanf ("%d", &x[ctr]);
   if(ctr>0)
   {
     for(j=0;j<ctr;j++)
     {
       if(x[j]==x[ctr])
        {
          flag=1;
printf("\n Number Already Exist!,Enter Another Value\n"); // add this 
break;
        }
       else flag=0; 
     } 

   }
   else flag=0;
 }while(flag==1);
}
}

Thanks to Vinitmittal for the answer... Two thumbs up!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.