vinitmittal2008 37 Junior Poster

Well as Narue suggested..
scanf function leaves '\n' into buffer, so when it reaches

while((character=getchar())!= '\n')

reads '\n' from buffer and loop terminates.
after this you are using

puts(sentence);

it will print some garbage value on screen bcoz your string is not terminated by a '\0' char.
to overcome these problems after using scanf function you need to flush input stream and after also terminate your string with '\0'.
Try this:

//Character Manipulation
#include <stdio.h>

void jsw_flush(FILE *in) // Function copied from older post of Narue
{
    int ch;

    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);

    clearerr(in);
}

int main()
{
int x;
//for loops
int counter=0;
//for input
char character;
char sentence[20];
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
scanf("%d",&x);
jsw_flush(stdin); // flush input buffer
switch(x)
{
case 1:
    printf("Enter something\n");
    while((character=getchar())!= '\n')
    {
      sentence[counter++]=character;
    }
    sentence[counter] = '\0'; 
    puts(sentence);
    break; 

}


}
vinitmittal2008 37 Junior Poster

Exactly what are you trying to do??

vedro-compota commented: +++++ +1
vinitmittal2008 37 Junior Poster

For that you need a good knowledge of programming, like you need to know worst case complexity, best case complexity and Average time complexity.
Read This

Snehamathur commented: Thanksss +1
vinitmittal2008 37 Junior Poster

Try this code..

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size (Between 1 to %d ): ",MAX);

    scanf("%d", &n); // User enters Array Size

    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1);
       scanf("%d", &array[i]); // Allows user to enter values into the Array
       }

    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("\n [%d] value is %3d ", i+1, array[i]); //Prints out the array -- this is where program stops it should carry on down
    }
     //**************************************//
    oddcount = 0;
    evencount = 0;

     for (i = 0; i < n; i++) // Code determines wheter odd or even.

     {
          if (array[i] % 2 != 0)
          {
            //printf("\nThe number %d Is Odd.\n", i);
            oddcount ++;
          }

          else
          {
            //printf("\nThe number %d Is Even.\n",i);
            evencount ++;
          }
     }
     printf("\n Odd Num: %d \n Even Num: %d", oddcount , evencount );

     return 0;
}

If you are still getting some problem then post your problem here..

vinitmittal2008 37 Junior Poster
# include <stdio.h>
# define MAX 100
int main(void)
{
    int array[MAX],i,n;
    
    printf("\n Enter Array size (1 - %d):",MAX);
    
    scanf("%d", &n);
    
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value :",i+1); 
       scanf("%d", &array[i]);
    }  
    
    // whatever else you want to do....... 

    return 1;
}

main function returns a integer vaule.. that is why return 1 is used.

vinitmittal2008 37 Junior Poster

Exactly what you want to do... if you just want to print * then it could be done by using a loop. scan a value from the user and run your loop like this..

int i,num;
// input a value here  from user in num..
for(i=1;i<=num;i++)
   putchar('*');
vinitmittal2008 37 Junior Poster

post your code here. We'll help you to solve your problem.

vinitmittal2008 37 Junior Poster

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);
}
vinitmittal2008 37 Junior Poster

Try this one also..

# include <stdio.h>

int main(){
    int a=10,b=3,reminder;
    // suppose you want a%b.

     reminder = a -(a/b)*b;
     
     printf("\n\n  Reminder of %d/%d is %d",a,b,reminder);
     return 0;
   }

This will also work like % operator..

Ancient Dragon commented: nice -- a lot better than mine :) +34
Snehamathur commented: Appriciated +1
vinitmittal2008 37 Junior Poster
//Function Protoype
char* stringCat(char *pWord, char *pTemp);

//Function call in main
 stringCat(stringCat(word, ": "), temp);
   printf("\n\nConcatenation: \"%s\".", word);
   printf("\n\n");

//Actual Function
char* stringCat(char *pWord, char *pTemp)
{ 
   int i;   
   int j;
   
   for (i = 0; pWord[i] != '\0'; i++);    // Notice the ;
   for (j = 0; pTemp[j] != '\0'; j++)
      pWord[i+j] = pTemp[j];
   pWord[i+j] = '\0';
   
   return pWord;
}
vinitmittal2008 37 Junior Poster

If Above is your question then the code could be...

#include <stdlib.h>
#include <stdio.h>

int main ()

{
    int Tab[5][3], i, j, GrandA = 0, GrandB = 0, GrandC = 0;
    int index_a=0,index_b=0,index_c=0,big_a,big_b,big_c;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            scanf("%d", &Tab[j][i]);
        }
    }

    printf("\n\n");

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("%d\t", Tab[j][i]);
        }
        printf("\n");
    }

    for(i = 0; i < 3; i++)
    {
        printf("\n");
        big_a = 0;
        big_b = 0;
        big_c = 0;
        
        for(j = 0; j < 5; j++)
        {
            if(big_a < Tab[j][0])
            {
                big_a = Tab[j][0];
                index_a=j;
            }

            if(big_b < Tab[j][1])
            {
                big_b = Tab[j][1];
                index_b=j;
                
            }
            if(big_c < Tab[j][2])
            {
                big_c = Tab[j][2];
                index_c=j;
            }
         }
        GrandA +=big_a;
        GrandB +=big_b;
        GrandC +=big_c;
        Tab[index_a][0]=Tab[index_b][1]=Tab[index_c][2]=0;
   
    }
    printf("%d ", GrandA);
    printf("%d ", GrandB);
    printf("%d ", GrandC);

 return 0; 
}

If its not your question then please describe it with example...

vinitmittal2008 37 Junior Poster

Just learn the use of strcpy, strcat and itoa function.. and it would be quite easy to implement such code..:)

Have a Look..

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main()
{
 char fname[12];
 char num[3]={"00"};
 int i;
 for(i=1;i<=20;i++)
  {
   strcpy(fname,"data-");
   if(i<10)
   itoa(i,&num[1],10);
   else
   itoa(i,num,10);
   strcat(fname,num);
   strcat(fname,".txt");
   puts(fname);

 }

 return 1;
}
vinitmittal2008 37 Junior Poster

Thanks for the suggestion... well now i tried this..

# include <stdio.h>

void show(int (*)[3],int,int);

int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

  show(a,3,3);
  return(0);
}
void show(int (*p)[3],int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

Is it right way??

Snehamathur commented: Nice & Clear +0