abhimanipal 91 Master Poster

Another solution could be something of this sort

int main(int argc, char* argv[])
{
        int i;
        scanf("%d",&i);
        if(i<=0)
        {
                printf("%d\n",i);                                        // This takes care of 0, -ve and alphabets
                printf("Invalid Input\n");
        }
        else
        {
                printf("%d\n",i);
                printf("Valid Input\n");
        }
}
abhimanipal 91 Master Poster

Just a thought ...
One of friends has a SONY Walkman phone. He can do simple functions (play/ pause the media player ) by using his cell phone. So that might be a place to start

abhimanipal 91 Master Poster

Make an array of the restaurant structures
Then sort according to the cost or what ever criteria that you want. You can use any of the common sorting algorithms for this
Only remember that each time that you swap, swap the entire structure and not just the cost

abhimanipal 91 Master Poster

Try Breath First Search.
Google for the code, I think you should be able to find it online

abhimanipal 91 Master Poster

You are right ... My mistake
The for loop is indeed correct

abhimanipal 91 Master Poster

Where did you get this code from ? Is this your code ?

1. The void measured_data_t function declaration is wrong. Correct that. Google for the correct syntax for function declaration
2. Delete the opening brace on line 16 and its corresponding closing brace.
3. I dont think line 18 is necessary

abhimanipal 91 Master Poster

There is a bug in your program.

When you receive a number, you straight divide by 10,000 and the result is stored in an integer. So if the input is 12,123.... This is how your code will process it

tthousands= 12,123/10,000
= 1.212

this value will be stored as 1 as tthousands is an int variable.
What I would suggest is that if the number is between 10,000 and 20,000 your first divide by 1000 and then switch on the result. After that you can process for hundreds and so on

abhimanipal 91 Master Poster

Thanks 'Ancient Dragon', modified code:

#include<stdio.h>
#include<sys/stat.h>
#include <sys/stat.h>

int main() 
{
        int i;
		char dirname[50];

        for(i=0;i<10;i++) {			 
				  sprintf(dirname,"E:/Visual C/practice/practice/hello/test%d",i);
                   if((mkdir(dirname,00777))==-1) {
                        fprintf(stdout,"error in creating dir\n");
                }
        }
}

Now my second issue :$ is that I would also like to create files inside each of these folders.

Here is my code on how to create files with text in them but I would like to place them inside the 'Folders' i.e. test0, test1....etc

main( )
{
FILE *fp;
FILE *patt,*patt1,*patt2,*patt3;
char stuff[25];
int index;

mkdir(const char *path, mode_t mode);

static const char text[26] = "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" 
                             "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z";

	  

fp = fopen("DATA.TXT","w"); /* open for writing */
patt = fopen("PATTERN.TXT","w"); /* open for writing */
patt1 = fopen("PATTERN1.TXT","w");
patt2 = fopen("PATTERN2.TXT","w");
patt3 = fopen("PATTERN3.TXT","w");
fprintf(patt,"ABCDEFGHIJ");
fprintf(patt1,"ZYXWVUTSRQ");
fprintf(patt2,"ZZZZZZZZZZ");
fprintf(patt3,"APKMNGHQUE");

for (index = 1;index <= 10000;index++)
fprintf(fp,text);
fclose(fp);
fclose(patt);/* close the file before ending program */
fclose(patt1);
fclose(patt2);
fclose(patt3);
}

Pattern file would probably remain the same but the length of text inside the data file will change

Quick question, Why are you using the for loop ?
Will the length of the the text array be dynamic or static ?

abhimanipal 91 Master Poster

Where exactly are you having a problem ? These are some of the mistakes that I could see
1. Line 8- Dont use typedef in front of the struct
2. Line 29- You have written 4 %d but then you are trying to input 5 things.
3. Line 46- Function definition, syntax is wrong
4. Line 55- For loop syntax is wrong

Make these changes, compile and run the code, then post where you are having problems

abhimanipal 91 Master Poster

Hello People,
Does any one have any practice problem sets for C. I am interviewing for a company tomorrow and they ask lots of "Whats the output ?" kind of questions..

Any help would be appreciated

abhimanipal 91 Master Poster

Hummm ...... Are you creating 10 children simultaneously ?

abhimanipal 91 Master Poster

Ok, but can I also do this?

for (j = 0 && choice = "yes"; j < 10 && strcmp (choice,"yes") != 0; j++) {
fgets (name[j], 10, stdin);
printf ("Do you want to enter more numbers? (yes or no)");
fgets (choice, 3, stdin); 
}

You cannot assign choice the value yes in the for loop. Either do it during initialization

char choice []="yes";

or use strcpy

char choice[10];

 for (j = 0 && strcpy(choice, "yes"); j < 10 && strcmp (choice,"yes")
abhimanipal 91 Master Poster

Oh, I think I understand how the functions in the header files work. Now is there a way for me to 'install' the missing code in the library file?

I'm not using Windows to run Cygwin. I just Googled Cygwin, and from my understanding Cygwin allows you to run Linux in a Windows environment. I am not trying to simulate a Linux environment, but should I be if I am playing around with semaphores?

You do not need that. Check out the MSDN link posted by Gerad in one of the earlier posts. Try running that code.

abhimanipal 91 Master Poster

How many child processes do you have to create ?
In the sense, you create 1 child process, it does the computation and sends the result back
Or the more complicated scenario
Each child computes only 1 row of the result matrix and you have to create as many children as their are rows

abhimanipal 91 Master Poster

Quick question , how are you making stars, archers, hearts appear on the screen ?

abhimanipal 91 Master Poster

Ok, I'll do that. Just a question, is this a correct fgets syntax?

fgets (name[j], 20, stdin);

And, why do I always have to use fgets, and not only instead of gets?
Do you want me to replace all the scanf with fgets?

You should avoid scanf as much as possible. Check out this link for more info on the difference between gets and fgets

http://wiki.answers.com/Q/Between_fgets_and_gets_which_function_is_safe_to_use_and_why

abhimanipal 91 Master Poster

A C program can change the file permissions as well. So you could do something of this sort.
Create a file
Write the data
Change permissions

Here is a link which gives more information for changing file permissions

http://www.delorie.com/gnu/docs/glibc/libc_290.html

abhimanipal 91 Master Poster

Why dont you create a simple text file. Put what ever data you want to put in that file.
After that you can use something like the chmod command

abhimanipal 91 Master Poster
void main()
{
    trie t;
    int ctr;

    for(ctr = 0; ctr < max; ctr++)
        t.(*letter)[ctr] = NULL;
}

You have to write

for(ctr = 0; ctr < 10; ctr++)
                t.letter[ctr]=NULL;
abhimanipal 91 Master Poster

One solution could be you could insert a '\0' /-1/some other invalid value after you are done inserting the numbers. Then in the function you can loop till you hit that invalid value

abhimanipal 91 Master Poster

I would advise you to use structures instead of so many arrays. Swapping structures is as easy as a=b
This will reduce the chances of error in your code

abhimanipal 91 Master Poster
int check_mat (int mat[N][M], int i, int j)
{
	int r,c;
	int flag = 1;

	for(r=i-1; r<i+1 ; r++)
	{
		for(c=j-1; c<j+1 ; j++)
			
				if(mat[i][j]>mat[r][c]&&IsInside(r,c))
								return 1;
						   			return 0;

	}
	
}

The for loop should be

for(r=i-1; r<=i+1 ; r++)
		for(c=j-1; c<=j+1 ; j++)

Otherwise you will miss the last row/columns

abhimanipal 91 Master Poster

The following code fragment from Programming Pearls:

for i = [0, n)
       if comlen(a[i], a[i+1]) > maxlen
              maxlen = comlen(a[i], a[i+1])
              maxi = i

I am not sure why he called the comlen function twice? Instead he could save the computed value in a temporary and use it to assign to maxlen. Isn't the function being called unnecessarily for the second time?

Quick question is this syntax not unusual for C ? Or is this some old/new version ?

abhimanipal 91 Master Poster


Or, knowing the limitation of int

What is this limitation ?

abhimanipal 91 Master Poster
struct host_msgs_list *root, *temp, *temp1;
root = NULL;
temp=root;

Are all these local variable or are these global variables ?

abhimanipal 91 Master Poster

In general you have do some thing of this sort

while(1)
{
      //Accept a new command from the user
     // Fork a new process
    // The child process handles the new command, finishes it and   returns the status to the parent

}
abhimanipal 91 Master Poster

Are you allowed to use relational operators ?

If yes you could do something of this sort

total_marks= 85                             //For example if the total marks are 85


printGrades(total_marks);


void printGrades(total_marks)
{
      i=total_marks; 
      for(;i>=90;)
      {
            printf("A\n");
            exit(1);
      }
       for(;i>=80;)
        {
             printf("B\n");
              exit(1);
        }
}
abhimanipal 91 Master Poster

lol.....

abhimanipal 91 Master Poster

Quick question can you do something of this sort ? Will help you to simplify the code ?

void calc()
{
      // Check which operation is to be done
      // Then call the appropriate function
}

Check this link for more information on function pointers
http://www.cprogramming.com/tutorial/function-pointers.html

abhimanipal 91 Master Poster

I have not programmed much on Turbo C, specially graphics.. But I have a couple of observations. I am 100% sure if these are indeed correct ... Please correct me if I am wrong

>>when I tried to run it, it suddenly closed

That's because there is nothing at the end of main() to prevent that behavior. If you want it to stay open so that you can see it, then add getche(); at the end of main(). The program will then stop until you hit a key.

He has a getch() on line 17. All the display functions are before that. So I think if all the functions are called correctly he should be able to see the output

Secondly

and when I tried to run it, it suddenly closed. and I don't know what is wrong with this. :

From this information I infer that he had a run time fault. Similar to the seg fault in the linux environment

abhimanipal 91 Master Poster

Is there any standard rules when we write the main function. I always thought the main function should always be written as

int main(int argc, char* argv)
{
     .....
     return 1;
}

But to my surprise, all these definitions are also correct . I got a compile time warning. But no compiler error or run time error. I am using gcc compiler

char * main()
{

}

char * main(int, char)
{

}

char * main(char)
{

}

Can anyone shed some light over this ?

abhimanipal 91 Master Poster
abhimanipal 91 Master Poster

Or you can use pointers. Some thing of this sort

int main(int argc, char* argv[])
{
        char *p;

        p="hellow World";
        printf("%s\n",p);
        return 1;
}
abhimanipal 91 Master Poster

You can check out this link. I think this contains sufficient information to get you started at least
http://en.wikipedia.org/wiki/Multiplexer

abhimanipal 91 Master Poster

Hello,
I have a conceptual code related to the C memory allocation model

if I have some code of this form, I will get a seg fault

int main()
{
     char* p;
     *(p+5)='A';
      printf("%c\n",*(p+5));
}

But when I have some code of this form I do not get a seg fault

int main(int argc, char* argv[])
{
        char* p=(char *)malloc(10);
        *(p+15)='A';
        printf("%c\n",*(p+15));

        return 1;
}

But I am unable to understand the reason for this. In both the case I am writing to unallocated memory. So why am I not getting a seg fault in the 2nd case

abhimanipal 91 Master Poster

Yet Another Version of Findind a Prime number:

#define CHK_NUM_IS_2_3_5( n )  ( n == 2 || n == 3 || n == 5 )
#define REM_2( n )  ( n % 2 != 0 ) 
#define REM_3( n )  ( n % 3 != 0 ) 
#define REM_5( n )  ( n % 5 != 0 ) 
#define CHK_REM_2_3_5( n ) ( REM_2( n ) && REM_3( n ) && (REM_5( n ) )           
                     
#include<stdio.h>

int main()
{
  int num ;
	
  REPEAT : 
    printf( "Enter a Num: " ) ;
    scanf( "%d", &num ) ;
    if( num <= 1 )
            printf(" ( %d ) is not PRIME \n " , num );
    else if  ( CHK_NUM_IS_2_3_5 ( num ) )
           printf(" ( %d ) is PRIME \n " , num );
   else if (CHK_REM_2_3_5 ( num ) )    
             printf(" ( %d ) is PRIME \n " , num );
  else
           printf(" ( %d ) is not PRIME \n " , num );
 printf(" Press < Enter > to continue..\n ");
 getchar();
 if ( getchar() < 0 )
	goto END;
goto REPEAT ;
END :
return 0;
}

i Hope this is the Efficient Version of All.
Any other ways, Plz Welcome.

I think your program will flag 49 as a prime number. 49 cannot be divided by 2, 3 or 5.

abhimanipal 91 Master Poster

Another option could be to use command line arguments

int main(int argc, char* argv[])
{
      // The values can be retrieved from argv. These values can be int, float, string. 
}
abhimanipal 91 Master Poster

Check out this link...This is posted as a job interview question

http://www.geekinterview.com/question_details/20527

This is the question I am trying to solve..... Still cannot understand it

If this came from a job-interview, you do not want to be working for this company. When they give you this just put on a disgusted face and walk away :)

I wish I could afford such luxuries..... Till then nose to the grindstone....Have to keep solving such stupid questions

abhimanipal 91 Master Poster

@nezachem

Why does scanf("%d %d"+2) become scanf("%d") ?

abhimanipal 91 Master Poster

@Salem
This is one of those problem sets which prepares you for the job interviews.

abhimanipal 91 Master Poster

Hello All,
I am unable to understand this piece of code

main()
{
     int i, j;
     scanf("%d %d"+scanf("%d %d", &i, &j));
     printf("%d %d", i, j);
}

I got this question from a C problem set. I run this code and I have to give the i/p 3 times. After this the code crashes. My questions are
1. What is the relevance of the + sign
2. Why do I have to give the i/p 3 times. Why not 4 ?
3. Why does the program crash ?

Thank You

abhimanipal 91 Master Poster

Then by your logic

int i= 2+3*5;

would evaluate to 25 .But it evaluates to 17 that is because the multiplication operation is done first, as it has higher precedence.

abhimanipal 91 Master Poster

Hello,

main()
{
      int i=4,j=7;
      j = j || i++ && printf("YOU CAN");
      printf("%d %d", i, j);
}

The answer to this question is 4 1 and the reasoning is that the compiler finds j to be true and hence does not need to check the remaining statement
But the precedence of the && operator is greater than the || . So the compiler should compute the operands of the && operator first right ?

abhimanipal 91 Master Poster

Got it ... Thanks man

abhimanipal 91 Master Poster

Hello Everybody,
I have just come across this particular piece of code in a C problem set

main()
{
      int i=5,j=6,z;
      printf("%d",i+++j);
}

The answer for this question is 11 because C evaluates this as i++ +j. But why cant this be evaluated as i+ ++j.
Is there any reason behind this ?

abhimanipal 91 Master Poster
union u
{
    union u
  {
      int i;
      int j;
  }a[10];
   int b[10];
}u;

main()
{
printf("n%d", sizeof(u));
printf(" %d", sizeof(u.a));
printf("%d", sizeof(u.a[4].i));
}

Hello People. I just came across this piece of C code. I have a couple of question
The third printf statement gives me the following error

pollux {~} > gcc test.c -o test
test.c: In function `main':
test.c:21: error: union has no member named `i'
pollux {~} >

I cannot understand the reason for this. Can some one shed some light on this ?