1. Become a Developer for CrazyEngineers - Apply NOW!

The CE Coding Challange

Discussion in 'Computer Science | IT | Networking' started by Sanyam Khurana, Sep 7, 2013.

  1. Aadit Kapoor

    Aadit Kapoor Apprentice

    Engineering Discipline:
    Computer Science
    The answer is correct if you do not believe me,try compiling the code in c
     
  2. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    First, I would like to know which compiler you are using?
    Second, don't just use any compiler, if you don't do it yourself, then there's no use (Personal Opinion)
    Third, Output will be:
    Compiler Error: “Unexpected end of file in comment started in line 7”.
    Explanation:
    The programmer intended to divide two integers, but by the “maximum munch” rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer,
    int k = *ip/ *jp;
    // give space explicity separating / and *
    //or
    int k = *ip/(*jp);
    // put braces to force the intention
     
  3. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Code:
    #include<stdio.h>
    int main() {
        static int i = 1;
        printf("%7d",i);
        i++;
        if(i <= 100)
            main();
        else
            return 0;
    }
     
  4. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Adding few more questions,

    Write a Program that divides two numbers without using / operator and also print it's remainder.

    Write the output of the following code fragment


    Code:
    #define int char
    void main()
    {
    int i=65;
    printf("sizeof(i)=%d",sizeof(i));
    getch();
    }
    
    Write a PROGRAM to find out square root of a number without using sqrt function.
     
  5. Jeffrey Samuel

    Jeffrey Samuel Star

    Engineering Discipline:
    Electronics
    Is the answer something like
    sizeof(i)=2

    Cos integer is 2 bit large and so expecting the answer to be 2
     
  6. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    nope! it'll be 1
     
    • Agree Agree x 2
  7. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Although you are close to the answer but this answer isn't right.

    Hint: Read the entire program carefully (from very first line to the very end), and I'm sure you'll come up with the right answer.

    One more question..
    Code:
    int a;
    void main()
    {
    int a;
    printf("%d....%d",::a,a);
    getch();
    }
    Do it without using Compiler
    write output without explaination.


    Absolutely Right..!
     
  8. Jeffrey Samuel

    Jeffrey Samuel Star

    Engineering Discipline:
    Electronics
    I got the reason why it must be one #define

    I missed it really a tricky one mate Thanks for this one
     
  9. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    i think the answer will be 0....<garbage value>
    as a is global so initial value is zero and we are printing scope of a that is global a
    and the next a is local a viz not initialized so it'll be a garbage value
     
  10. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Absolutely Right
    Even I missed it mate, and the reason is it's not tricky, but we get so much excited, that in over excitement we don't look at the code properly and unfortunately do errors.

    Yes, it will first print 0 because of global variables are initialised to zero, and then print some garbage value corresponding to the local variable a.
     
    • Agree Agree x 1
  11. Aadit Kapoor

    Aadit Kapoor Apprentice

    Engineering Discipline:
    Computer Science
    I am using clang compiler.It is obvious that now one writes int k = *ip/*jp;
    Every programmer gives space.I am not using any compiler.I first posted the answer then when you told me it is wrong i checked it with a compiler.

    Basic rules:
    FIRST: C does not have ::(Scope resolution operator).
    SECOND: Please the full code.

    The answer is garbage values for both of the a's
     
    Last edited by a moderator: Oct 2, 2013
  12. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    no one said that this thread is ONLY for C programming language
     
  13. Aadit Kapoor

    Aadit Kapoor Apprentice

    Engineering Discipline:
    Computer Science
    I Agree,but we we can't write mixed code.We cannot mix C and C++.
     
  14. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Design a program to simulate a calculator, but wait, not that one with the old simple sweet switch case asking what you have to do with two values.

    Instead, we want something like this,

    Say, a person wants to add 5 to 6, so he would just write 5+6 on the console and he receives the output 11.

    Similarly for multiplication 5*6, should return 30 multiplying the two numbers.

    Likewise for division and subtraction.
     
  15. Aadit Kapoor

    Aadit Kapoor Apprentice

    Engineering Discipline:
    Computer Science
    #include <stdio.h>
    #include <stdlib.h>
    #define CHECK_FOR_ZERO(x) { if (x==0) printf("Cannot divide by zero! ERROR 100\n"); exit(0);}
    /* A simple Calculator */
    /* Created by Aadit Kapoor */
    int main()
    {
    int check = 0; /* For while loop */
    while (check == 0)
    {
    int n,n2; /* Numbers */
    char op; /* Operator */
    printf("Enter numbers (type '0q0' to quit): ");
    scanf("%d%c%d",&n,&op,&n2); /* In form (number-operator-number) */
    if (op == '+')
    {
    printf("Calculated Result = %d\n",n + n2);
    }
    else if(op == '-')
    {
    printf("Calculated Result = %d\n",n-n2);
    }
    else if(op == '*')
    {
    printf("Calculated Result = %d\n",n * n2);
    }
    else if (op == '/')
    {
    CHECK_FOR_ZERO(n2); /* CHECK_FOR_ZERO(x) */
    printf("Calculated Result = %d\n",n / n2);
    }
    else if(n == 0 && op == 'q' && n2 == 0)
    {
    printf("Bye!\n");
    exit(0);
    }
    else
    {
    printf("Invalid operation!\n");
    printf("Try again!\n");
    }
    }
    return 0;
    }
     
  16. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Nice!
    But please place your quote in the quote tag always.
    Ok, one more quetion CEans..

    Code:
    void main()
    {
    struct student
    {
    char name[30], rollno[6];
    }stud;
    FILE *fp = fopen(“somefile.dat”,”r”);
    while(!feof(fp))
    {
    fread(&stud, sizeof(stud), 1 , fp);
    puts( stud.name);
    }
    }
    Explain the output with explanation.
     
  17. avii

    avii Ace

    Engineering Discipline:
    Computer Science
    Print 1 to 100, without using loops, recursions in C.
     
  18. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Already solved here, by @Vishal0203 in post #123

    EDIT: Oops, my bad, it uses recursion.
    Can we use goto in this?
     
  19. avii

    avii Ace

    Engineering Discipline:
    Computer Science
    ^no. No go to also.
     
  20. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Okay this is another way! it doesn't use goto, recursion, no loops!
    unlike my post #123, in this main() doesn't call itself, it is called by printer() so it doesn't use recursion!


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int printer();
    int i = 1;
    int main() {
        printf("%4d",i);
        printer();
    }
    
    int printer() {
        i++;
        if(i > 100)
            exit(0);
        main();
    }
     
    • Like Like x 2

Share This Page