Discussion in 'C Programming' started by Girish Pal Singh, Jul 25, 2003.

  1. I will be greatful if any one answers these questions?
    Thank You.
    1. Write a "Hello World" program in 'C' without using a semicolon.
    2. Write a C++ program without using any loop (if, for, while etc) to
    print numbers from 1 to 100 and 100 to 1;

    3. Find if the given number is a power of 2.
    4. Multiply x by 7 without using multiplication (*) operator.
    5. Write a function in different ways that will return f(7) = 4 and
    f(4) = 7
    6. Remove duplicates in array
    7. Finding if there is any loop inside linked list.
    8. Remove duplicates in an no key access database without using an
    9. Convert (integer) number in binary without loops.
    10. Write a program whose printed output is an exact copy of the
    source. Needless to say, merely echoing the actual source file is
    not allowed.
    11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
    each player selects a number and adds it to the total. Once a
    number is used, it must be removed from the pool. The winner is the
    person whose number makes the total equal 31 exactly.
    13. Swap two numbers without using a third variable.
    Girish Pal Singh, Jul 25, 2003
    1. Advertisements

  2. Do your own homework.
    Hallvard B Furuseth, Jul 25, 2003
    1. Advertisements

  3. Girish Pal Singh

    Neil Cerutti Guest

    These questions?
    You're welcome.

    How'd I do?
    Neil Cerutti, Jul 25, 2003
  4. Girish Pal Singh

    Bob Day Guest

    1. Do your own homework.

    2. It's 'grateful', not 'greatful'.

    3. Write C code to divide by 15 efficiently without
    using a divide operator.
    You're welcome.

    < snip >

    -- Bob Day
    Bob Day, Jul 25, 2003
  5. Girish Pal Singh

    Peter Ammon Guest

    Try using the token pasting operator on the comma and period.
    I asked my computer, and it came up with this:

    #include <stdio.h>
    int main(void) {
    int i;
    puts("#include <stdio.h>\nint main(void) {");
    for (i=1; i<=100; i++) printf("puts(\"%d\");\n", i);
    for (i=100; i>=1; i--) printf("puts(\"%d\");\n", i);
    puts("return 0;\n}");
    return 0;

    If my computer can solve this, you can too.
    int isPowerOf2(int x) {
    int i;
    for (i=0; x > 0; i++) {
    if (x==1) return 1;
    if (isPowerOf2(i)) x-=i;
    return 0;

    I am tremendously proud of this solution. Finding such a terse solution
    with disgustingly exponential time isn't easy.
    foo(x) { return x==4 ? 7 : 4; }
    foo2(x) { return x==7 ? 4 : 7; }
    void removeDuplicates(int* array, int_t count) {
    int i;
    for (i=0; i < count; i++) array=i;
    I couldn't come up with a tongue in cheek but correct answer for this
    one. Anyone want to take it? (The usual algorithm is to traverse the
    list at different speeds and see if you ever meet up).
    What the heck is a "no key access database?"
    Convert it to what?

    (The program is between the quotes.)

    int main(void) {
    int first_number=1;
    int second_number=2;
    int third_variable;
    int fourth_temp;
    return 0;

    Notice I never use the third variable. Want proof?

    peterammon: ~ ) cc -W -Wall test.c
    test.c: In function `main':
    test.c:4: warning: unused variable `third_variable'

    Peter Ammon, Jul 25, 2003
  6. Girish Pal Singh

    Malcolm Guest

    I wonder what you tutor is looking for here. Maybe these are end of term
    "fun" questions;
    Brute force this is easy. Try clc++ for further hints.
    You need to check that only one bit is set.
    This is called backcracking
    x * constant
    can always be replaced by
    (x << a) + (x << b) ...
    presumably without using an if statement. You need to look at bitwise
    This is actually a real task. It can be done by using nested loops.
    You need to store the address of one of the nodes, then check if you revisit
    Got me here. "Never bullshit your way into a databse job".
    This is a stupid hack. I'll give you this one.

    a ^= b; a ^= b; a ^= b.
    Malcolm, Jul 25, 2003
  7. Girish Pal Singh

    Eric Sosman Guest

    int main(void) {
    puts ("Hello, world!") .,
    return 0 .,

    Unfortunately, this example won't compile or execute.
    But the problem statement only asked that the program be
    written, not that it be correct, so that's all right.
    Sorry; this is comp.lang.c, not comp.lang.c++. Ask your
    C++ questions somewhere else -- comp.lang.snobol, perhaps.
    int is_power_of_2(int given_number) {
    return given_number > 0;
    int multiply_by_7(int x) {
    return x / 0.14285714285714286;
    int f(int x) {
    return x == 4 ? 7 : 4;

    int f(int x) {
    return x == 7 ? 4 : 7;
    for (i = 0; i < array_elements; ++i)
    array = i;
    int has_a_loop(struct list_node *ptr) {
    for ( ; ptr != NULL; ptr = ptr->next)
    return 0;

    If the list is loop-free, this function returns zero
    ("false"). If there *is* a loop, the function does not
    return zero.
    Databases are off-topic in comp.lang.c. Try another
    newsgroup, like comp.graphics.algorithms.
    void to_binary(unsigned int x) {
    static const char *binary[] = { "0", "1", "10",
    "11", "100", /* complete in the obvious way */
    puts (binary[x]);
    This is impossible. If a program does not output its own
    source file, it hasn't output its own source, merely something
    that happens to look exactly like it. The fact that two files
    have identical contents doesn't make them the same file, just
    as the fact that two dollar bills may look identical but aren't
    the same dollar bill -- in fact, if they *do* look identical
    it follows that at least one is a forgery. Q.E.D.
    void play_and_win(void) {
    puts ("I go first. I choose the four sixes,\n"
    "a four, a two, and a one, and add them\n"
    "all to the total. The total is now\n"
    "thirty-one, so I win! Hooray!");
    void swap(int x, int y) {
    printf ("x = %d, y = %d\n", y, x);
    Eric Sosman, Jul 25, 2003
  8. Girish Pal Singh

    Paul Hsieh Guest

    int linkListLoop (struct linkedList * x) {
    x->next = x;
    return 1; /* Indicating that yes it has a loop in it. */
    Paul Hsieh, Jul 26, 2003
  9. Girish Pal Singh

    Andy Zhang Guest

    Easier way would be to use log:

    #include <math.h>

    int isPowerOf2(int x)
    return (fmod(log10(x), log10(2))) == 0;
    Andy Zhang, Jul 26, 2003
  10. Girish Pal Singh

    Martijn Guest

    3. Write C code to divide by 15 efficiently without
    define "efficiently" :)
    Martijn, Jul 26, 2003
  11. how about this for a symmetrically rounded division routine for unsigned

    typedef unsigned int u;
    u uidiv(u D,u d){u q=0,c=1;for(;d<D
    q+=(d<=D?D-=d,1:0);return q+(D>d);}

    if you want efficiency, make D,d and q registers :p
    Pieter Droogendijk, Jul 26, 2003
  12. It's not a question.
    What is C++?
    None of thee are questions.

    Do you have a question about the C language?
    Emmanuel Delahaye, Jul 26, 2003
  13. Please tell me when your homework is due, and I'll provide the answers
    immediately after that time.

    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "A bicycle cannot stand up by itself because it's two-tyred."
    - Sky Text
    Joona I Palaste, Jul 26, 2003
  14. Girish Pal Singh

    Martijn Guest

    4. Multiply x by 7 without using multiplication (*) operator.

    See the post by krazyman a week ago.
    Martijn, Jul 26, 2003
  15. Girish Pal Singh

    Tim Woodall Guest

    if(strstr("linked list","loop")) printf("yes\n");
    No idea but
    DROP DATABASE <dbname>;
    ought to do the trick.

    Tim Woodall, Jul 27, 2003
  16. Girish Pal Singh

    MCheu Guest

    Do a google search. Homework questions come up quite frequently, and
    while against the general consensus, some people do post answers
    occasionally (the questions are quite common homework questions).

    If you're going to use those to cheat though, you should at least look
    them over and be prepared to do some debugging. Some are
    intentionally wrong (though not always obviously so), others work but
    are so obfuscated that you'd have to prove to the TA that you know how
    it works to get the grade for it.

    All things considered, it might actually be easier to do it yourself.
    Might save you some time in studying for the exam later too.
    MCheu, Jul 27, 2003
  17. Girish Pal Singh

    pete Guest

    This is the simple way:

    int isPowerOf2(unsigned n)
    return n && !(n & n - 1);
    pete, Jul 28, 2003
  18. Cool Marsha Clark reference. I remember that: "Neener, neener, neener."
    heh heh
    Steve Zimmerman, Jul 28, 2003
  19. Girish Pal Singh

    Peter Ammon Guest

    On my machine, this function fails to report many values as being powers
    of 2, including 8, 32, 64, 128, 512, 1024, 2048.... Don't trust
    floating point precision.

    Peter Ammon, Jul 28, 2003
  20. Girish Pal Singh

    Kevin Handy Guest

    int divide(int a, int b)
    return (int)pow(10.0, log10((double)a)-log10((double)b));

    assuming efficient hardware assisted pow, log10, conversions,
    values passed being in proper range, etc.

    Now define efficient, or any additional silly limitations.
    Kevin Handy, Aug 7, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.