1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C Programming - First assignment not sure how to approach

  1. Sep 22, 2015 #1
    1. The problem statement, all variables and given/known data
    Hi, I am currently in a C programming course and i have no background knowledge of programming whatsoever. The professor in my course isn't very good at explaining so I'm a bit lost here. (Please keep in mind, I am NOT asking anyone to do this for me. I would like to know a good approach on how to tackle these assignments.)

    The variance (SD2 ) of a number (N) of scores in a population tells us how spread out the scores (X’s) are around the mean of the scores (M). The variance is the average of the squared deviations from the mean or the standard deviation squared and thus, the reason the symbol SD2 is used to represent variance. The standard deviation (SD) is the square root of the variance. The variance is the average of each score’s squared difference from the mean. The formulas for the variance (SD2 ) and the standard deviation (SD) are given below. Variance = SD2 = ∑(X – M)2 / N Standard Deviation = SD = √SD2

    Note that the variance formula used in this assignment is the exact number of scores N and not N – 1 that may be used in some variance formulas found elsewhere. Also, in C, you can use the built-in function sqrt(M) to get the square root of M and you can simply multiply a number M by itself to get the square of M or M2 .

    1. Write an algorithm and a program that reads five given real scores and computes the variance and standard deviation of 5 given scores and displays the following:
    i. For each input score, a row showing the Score (X), the Mean Score (M), deviation score (X – M) and squared deviation score (X – M)2 .
    ii. A row showing the sum of deviation squared ∑(X – M)2 .
    iii. A row showing the Variance (SD2 ) of the scores
    . iv. A row showing the standard deviation (SD) of the scores.

    I really don't understand what to do here. I don't even understand what's going on, its our second class so far and she hasn't taught us anything aside from general information of how computers evolved, so I have no idea how to program. The course is apparently "for beginners who have no background knowledge" (namely me). But simply put, this is our first introduction to programming. Again, she has not taught a single thing about programming, not even the basics.

    2. Relevant equations

    ?
    3. The attempt at a solution

    Questions:
    1) When it asks you to write an algorithm, does this mean the general methods and sequence they should be executed to solve the problem? So am I to solve the problem literally through mathematical means and then simply put them in a document? Or is there more programming involved here

    2) Am I supposed to know how to code this? Is there a general approach here that I'm missing?

    I apologize for the wall of text. Again, any help is appreciated. I'd just like some light shed for this so I can do the assignments in the future properly.
     
  2. jcsd
  3. Sep 22, 2015 #2

    DEvens

    User Avatar
    Education Advisor
    Gold Member

    https://en.wikipedia.org/wiki/Algorithm

    If this was not a programing class, and you had no thought of doing this in a computer program, could you write down the procedure to do what this assignment asks?

    So could you write down steps like the following? Get the five numbers, make this total, get the squares of each of the five, get this total, and so on.

    That set of steps (assuming it is correct and complete and works) is your algorithm. Once you have that then, and only then, should you consider starting to write your program. Make sure it is literally step-by-step.

    Once you get that you can start trying to code it up in C. That will mean you need to know quite a bit about how to do things in C.
    - How to create a source file that holds your computer program. This could be some simple text editor for example.
    - How to compile and run a C program. The first example is usually called HelloWorld.C by long tradition.
    - How to read in numbers from some kind of input. This could be a file or typed in from the terminal.
    - How to declare variables to hold values. You should know, at a minimum, how to make an integer variable and how to make a floating point variable.
    - How to do loops.
    - How to write formulas to calculate values based on variables, and put those values in other variables.
    - How to write numbers out to some kind of output. This could be to a file or typed out onto the terminal.

    Learning this much, from a standing no-experience-at-all start, should be some few hours of class time. Say more than three but probably less than ten. If all of that really looks unfamiliar, you are not ready to do the coding part of this assignment.

    A possible assisting resource:

    https://www.amazon.com/Schaums-Outline-Programming-Byron-Gottfried/dp/0070240353/

    This is Schaum's C book. Cheap and introductory in a "solved problem" format. Don't let it be your only C book though, since it has some, well, quirks. But it will help you up the learning curve.
     
  4. Sep 22, 2015 #3

    Mark44

    Staff: Mentor

    When is the assignment due? If it isn't due for several weeks, your instructor will likely cover the topics involved in writing a C program.
    The problem statement asks you to write an algorithm and a program. The algorithm is a set of steps to be performed by your program. These will generally entail getting input from the program user, doing the calculations for the mean, deviation, and square of the deviation, etc. After these are calculated, your program should output its results in an organized manner.

    That's a sketch of the algorithm -- you'll need to fill in a few more details. The program you write should be the implementation (in C code) of that algorithm.
    I don't see how you could be expected to know how to code this just yet. As I said earlier, maybe the due date is some time away, and the instructor will provide more information during that time.
     
  5. Sep 22, 2015 #4
    Thanks alot for the replies. Good to know that my intuition isn't worthless and I did need to have some background knowledge. Basically my teacher handed this assignment out yesterday, and it is due this thursday. I do not have a class(I do have a lab tomorrow, however I sincerely doubt she will address anything) until next monday. After reading some of her reviews online as a professor, it is safe to say this class will be completely independent learning on my part(since im a first year, I guess this is a relatively good experience.)

    So it looks like I'll be purchasing a copy of that book you have recommended online and will be using that as a basis for now. Do you have any other recommendations of a good book? Assuming price is not an object.

    Just to make sure, does an algorithm need to follow a specific template? I'm looking through the textbook of my class(mind you it was written by the professor) and it seems... a little odd. Can I not just write a complete guide with proper english on how to solve the problem?

    Just a sample:
    Input: Score1, Score2, ... Score5
    Output: Mean, DeviationScore, SquaredDeviation

    Steps:
    1: Find the mean of the scores. This can be done by finding the sum of each test score and dividing it by the number of scores present.
    2: Using the mean obtained in step 1, find the deviation scores(X - m) with each respective score to the mean.
    3: Using the deviation scores found in step 2, square each respective deviation score.
    4: Using the squared deviation scores from step 3, find the mean of such by finding the sum and dividing it by the number of scores present.
    5: Find the square root of step 4.

    Is that okay for an algorithm or is that simply garbage? Thanks for the replies again, if you have any recommendations for a good introductory book, please let me know.
     
  6. Sep 22, 2015 #5

    berkeman

    User Avatar

    Staff: Mentor

    Your algorithm under your Spoiler tag looks reasonable. Just out of curiosity, why do you keep posting stuff under Spoiler tags?
     
  7. Sep 22, 2015 #6
    It looks more organized to me haha
     
  8. Sep 22, 2015 #7
    Alright so I've dabbled along the internet, I've found code online of what I'm looking for(or close to it) and I've been trying to intrepet it from my end. (Since it seems ill have to improvise considering the due date is on thursday...) Again please excuse my ignorance where necessary. I've only started reading in to the actual programing aspects of the language a few hours ago

    http://www.sanfoundry.com/c-program-mean-variance-standard-deviation/

    Line 3 is to set a maximum for characters
    Line 7 is to set variable x to maxsize
    Line 8 labels i and n as integers
    Line 9 is declaring variables, unsure of what sum=0 and sum1= 0 is however.
    Line 11 outputs a message that tells the reader to type a value, then enters a new line
    Line 12 is for input of the number
    Line 13 is for telling the reader to input how many scores there are
    Line 14 I don't understand. I know that it means that if there is a value of 0 for i(and if n is greeeeeater than i, then i will increase in increments of 1? does this mean i will be instantly increased to 1?)
    Line 16 I don't know.
    Line 19 why is this being added again for line 14?
    Line 21 Is adding integer I to something? I don't konw.
    Line 23 This line is for determining the mean
    Line 25 Again this line appears
    Line 27 I'm assuming this is (X-m)^2
    Line 29 Determines the mean of line 27
    Line 30 Determines the standard deviation
    lines 31-33 relays the information to the reader.

    1 question I have is how would one be able to organize the relayed information into a table?

    Thanks for your time.
     
  9. Sep 23, 2015 #8

    Mark44

    Staff: Mentor

    When you post code, please paste it directly into the page here. It's difficult for me to type a response in this window, but have to switch back and forth to another window.

    BTW the line numbers you cite are all different from the line numbers on the web page.
    No. It's the maximum number of integers your program will be working with. Integers (type int) and characters (type char) are different types.
    No. x is an array with MAXSIZE elements. That's different from what you wrote above.
    This line declares i and n as int variables. This tells the compiler to set aside storage in memory for these variables.
    In this line, sum and sum1 are declared and initialized to 0. You always have to do this when you have a variable that will accumulate values to find their sum.
    The message prompts the user to type a value. The \n control character (it's considered a single character, even though there are two symbols) causes a new line to be printed, leaving the cursor at the beginning of the next line on the screen.
    Yes. This is a little tricky in C, as you have to provide the address (&n) of the location at which the value will be stored.
    It is echoing the value that the user just typed.
    This is the header for a for loop. It starts off with a value of 0 for i, and checks that this value is less than n. If so, it executes the body of the for loop.
    I don't know either. What's the line number on the web page?
    I don't see anything like what you're describing.
    Something like that
    Yes
    You'll need to use printf() and format your output in columns.
     
  10. Sep 23, 2015 #9
    I apologize for that... I didn't realize the lines weren't identical(you did get all of them except for two though) Thanks for taking the time to read through all of my evaluations.

    Line 16 = Line 20
    Line 21 = Line 25

    I have actually written my own piece of code, and for what its worth it works. I think it is a slight bit unorganized but I have to essentially write code without calling any functions(this is part of the rules for the assignment)


    #include <stdio.h>
    #include <math.h>
    // limit
    int main(void)

    {

    float X1,X2,X3,X4,X5;
    //Output Data
    float Variance,Sum,SD, D1sqr, D2sqr, D3sqr, D4sqr, D5sqr, M,D1,D2,D3,D4,D5;

    printf("Please print 5 test scores \n");
    scanf("%f %f %f %f %f", &X1,&X2,&X3,&X4,&X5);

    M = (X1 + X2 + X3 + X4 + X5)/5.0;
    D1 = X1 - M;
    D2 = X2 - M;
    D3 = X3 - M;
    D4 = X4 - M;
    D5 = X5 - M;
    D1sqr = D1*D1;
    D2sqr = D2*D2;
    D3sqr = D3*D3;
    D4sqr = D4*D4;
    D5sqr = D5*D5;
    Sum = D1sqr + D2sqr + D3sqr + D4sqr + D5sqr;
    Variance = (D1sqr + D2sqr + D3sqr + D4sqr + D5sqr)/5.0;
    SD = sqrt(D1sqr + D2sqr + D3sqr + D4sqr + D5sqr)/5.0;

    printf("Score \t-\t Mean \t=\t Deviation Score \tSquared Deviation\n");
    printf("(X)\t\t(M)\t\t(X-M)\t\t\t\t(X-M)squared\n");
    printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X1,M,D1,D1sqr);
    printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X2,M,D2,D2sqr);
    printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X3,M,D3,D3sqr);
    printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X4,M,D4,D4sqr);
    printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X5,M,D5,D5sqr);
    printf("\t\t\t\t\t\tSum: %.2f\n",Sum);
    printf("Variance: = %.2f/5 = %.2f\n",Sum,Variance);
    printf("Standard Deviation = SD = sqrt(%.2f)\n",sqrt(Variance));
    return 0;

    }

    The only issue is that at the 2nd last line of code actually prints "√variance" rather than the actual value of the variance. E.g., if the variance is meant to be 20, it'll show up as √20. How can i get it to show the variance without the square root symbol to the user?

    Thanks alot.

    (P.s., i was also wondering if Dennis Ritchie's book of the C programming language was a good basis for learning. Wasn't sure if it was outdated)
     
  11. Sep 24, 2015 #10

    Mark44

    Staff: Mentor

    For that line of code, change this:
    Code (Text):
    printf("Standard Deviation = SD = sqrt(%.2f)\n",sqrt(Variance));
    to:
    Code (Text):
    printf("Standard Deviation = SD = %.2f\n",sqrt(Variance));
    Are you saying that it actually prints a √ symbol? That seems pretty odd to me.

    Regarding the book -- if you're thinking of the one I'm thinking about, it's called "The C Programming Language," by Brian Kernighan and Dennis Ritchie. It's a very good book, but IMO it's not the best choice for brand-new programmers. It is an excellent reference, though.
     
  12. Sep 24, 2015 #11
    Yes, it would literally print out √<value of the variance>. I tried the line of code you gave me and it works. Thanks alot!

    And yes you're right about the book. I think I'll avoid it for now until I've finished this class(I'm just looking for a variety of books to rely on since it seems learning will be 100% independant for me)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C Programming - First assignment not sure how to approach
Loading...