Hi needs some help with short C programming Questions?

This is a discussion on Hi needs some help with short C programming Questions? within the C Programming forums, part of the General Programming Boards category; Hi, i sort of know the answer to these questions but im not sure exactly .. any help wud be ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    11

    Hi needs some help with short C programming Questions?

    Hi, i sort of know the answer to these questions but im not sure exactly .. any help wud be appreciated ..
    1)Reproduce layout of following programe
    Code:
    #include <stdio.h>
    #define NMAX 10
    int main()
    {
    unsigned int n, m=5;
    int value [NMAX]={0};
    char label[] ="data";
    for(n=0; n<NMAX; n++);
    {
    value[n]= n % m;
    printf("%s[%2u]=%2d\n", label, n, value[n]);
    }
    return 0;
    }
    2)write a short C prgram that will carry out the following:
    a)repeatedly prmpt the user to enter a real number, exiting when the user enters the value 0.0 and
    b)enter running total of all numbers entered

    3)what info does each fo the following C statements provide the comiler
    struct vector{float x; float y; float z;};
    struct vector position
    struct vector Cross_product( struct vector a, struct vector b);

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Hi, i sort of know the answer to these questions but im not sure exactly
    Most people will tell you to post your attempts to these problems. It's great that you already have (or said you) tried. So post all of your complete attempted solutions to each problem, then we have a place to start helping you. Also,
    1)Reproduce layout of following programe
    I have literally no clue what this is asking.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That means "put these lines of code into your program" - I think!

    That code was given to him. C'mon, Jack0, put some "skin" into this assignment.
    Last edited by Adak; 03-06-2010 at 01:15 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    lol
    it means right out its output, from wot i gather its something like ;
    data[10]=0 but like repeated bcoz its a loop ?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    OK, so you write out what the output would be. You said you have an idea of what the solutions for all the problems are, but you still have to post your attempted solutions!

    The first one loops 10 times, right? And each iteration of the loop it prints a line of output. So write out what those 10 lines would be, by manually following the logic of the code. Easiest thing to do is to grab a piece of paper, and follow the code as if you were the computer executing each statement.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    data[0]=value[0]
    data[1]=value[1]
    data[2]=value[2]
    ....
    data[9]=value[9]
    data[10]=0

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    value[n]= n % m;
    The "%" is the modulus operator, so the values will only be those in the range of [0,5), or 0, 1, 2, 3, 4. However, notice the line
    Code:
    for(n=0; n<NMAX; n++);
    There is a semi colon after the for loop, which means the for loop has no body.

    I really can't tell if you're making an effort to get our help. Until you can make me believe that, I can't really be bothered to continue to try to help.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    1) ohh ok i understand now .. because value(n)=remainder of n/m .. data[n]= would go from 0 to 5 then start again .. thanks alot i get this one now ..
    2) for this one am i supposed to use an if statement and while loop ?
    3)
    Code:
    struct vector{float x; float y; float z;};
    does this just mean a structure of type vector containing real numbers x, y, z ?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    For 1), the items go from [0,5). This is mathematical notation for saying values between 0 and 5, including 0 but excluding 5. So the numbers are 0, 1, 2, 3, 4, as I said above. It does not include 5.

    For 2), as with any other problem, there is generally more than one solution. So you can achieve the same end result in a number of different ways: if you want to use an "if" statement and a "while" loop then go for it. Just start trying it out, and if it doesn't work try another method. If it still doesn't work then post the code and describe the problem to us.

    For 3) I don't really know what they expect for an answer. The only hint I could give would be the actual answer that I think they expect, so that doesn't help you by me just giving the answer. But you seem to be on the right track describing that one line, except that it's defining a "struct" named "vector" (this is different from what you said), containing the variables you've said.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    2)
    Code:
    #include <stdio.h>
    
    int main()
    {
        float n;
    
        printf(" enter a value\n");
        scanf("%f\n", &n);
        if(n>0) { while(n>0) {
            printf(" enter a value\n");
        scanf("%f\n", &n);
        }}
        else return 0;
        }

    is this right ? and/or is there a simpler way of doin this ?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The if/else statement is redundant; you don't need it. Also, the formatting is poor. I will "fix" (subjective) the formatting and remove the if/else block, giving
    Code:
    #include <stdio.h>
    
    int main()
    {
        float n;
    
        printf(" enter a value\n");
        scanf("%f\n", &n);
    
        while(n>0)
        {
            printf(" enter a value\n");
            scanf("%f\n", &n);
        }
    
        return 0;
    }
    I haven't changed anything else. The requirement you are given is to stop looping if the number is equal to 0. So the negation of that, and thus your "while" loop condition is "n !=0", not "n > 0". Also your assignment says you must have "running total". So you need a "sum" variable, initialized to 0. On each iteration you'd add the user's number to the sum/running total. Also, at the end of the program I imagine they want the running total to be printed.

    So work on these things and let us know if you have further problems. If so, post your complete code and describe the exact problem.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    
    int main()
    {
        float n;
        float sum=0.0;
    
        printf(" enter a value\n");
        scanf("%f", &n);
    
    
        while(n!=0) {
            printf(" enter a value\n");
        scanf("%f", &n);
        sum += n;
        }
        printf("\nsum is equal to %f\n", sum);
    
        }
    i cant get it to include the first "n" outside the loop

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I dont know what you mean by your question. I haven't looked at the code, but maybe your having problems with Cprogramming.com FAQ > How do I get a number from the user (C).

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    no basically it only sums up the values inside the loop but not the first value entered before the loop was made..

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Try and change the order of the statements in the while loop, from
    Code:
        while(n!=0) {
            printf(" enter a value\n");
            scanf("%f", &n);
            sum += n;
        }
    to
    Code:
        while(n!=0) {
            sum += n;
            printf(" enter a value\n");
            scanf("%f", &n);
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 02:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM
  5. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 12:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21