# C Programming - First assignment not sure how to approach

• Arnoldjavs3
In summary, the conversation is about a student who is struggling in a C programming course with no prior knowledge and a professor who has not taught any programming basics. The student is seeking help on how to approach their assignments, particularly understanding what is meant by writing an algorithm and coding a program. The expert suggests that an algorithm is a set of steps to be performed by the program and the student will need to learn basic coding skills before attempting to write their program. They also recommend using additional resources such as a C book to supplement their learning.
Arnoldjavs3

## Homework Statement

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.

?

## 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.

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/dp/0070240353/?tag=pfamazon01-20

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.

Arnoldjavs3 said:

## Homework Statement

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.
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.
Arnoldjavs3 said:

?

## 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
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.
Arnoldjavs3 said:
2) Am I supposed to know how to code this? Is there a general approach here that I'm missing?
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.
Arnoldjavs3 said:
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.

Thanks a lot 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 I am 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.

Your algorithm under your Spoiler tag looks reasonable. Just out of curiosity, why do you keep posting stuff under Spoiler tags?

It looks more organized to me haha

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 into 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.

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.
Arnoldjavs3 said:
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 into 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
No. It's the maximum number of integers your program will be working with. Integers (type int) and characters (type char) are different types.
Arnoldjavs3 said:
Line 7 is to set variable x to maxsize
No. x is an array with MAXSIZE elements. That's different from what you wrote above.
Arnoldjavs3 said:
Line 8 labels i and n as integers
This line declares i and n as int variables. This tells the compiler to set aside storage in memory for these variables.
Arnoldjavs3 said:
Line 9 is declaring variables, unsure of what sum=0 and sum1= 0 is however.
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.
Arnoldjavs3 said:
Line 11 outputs a message that tells the reader to type a value, then enters a new line
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.
Arnoldjavs3 said:
Line 12 is for input of the number
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.
Arnoldjavs3 said:
Line 13 is for telling the reader to input how many scores there are
It is echoing the value that the user just typed.
Arnoldjavs3 said:
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?)
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.
Arnoldjavs3 said:
Line 16 I don't know.
I don't know either. What's the line number on the web page?
Arnoldjavs3 said:
Line 19 why is this being added again for line 14?
Arnoldjavs3 said:
Line 21 Is adding integer I to something? I don't konw.
I don't see anything like what you're describing.
Arnoldjavs3 said:
Line 23 This line is for determining the mean

Line 25 Again this line appears
Arnoldjavs3 said:
Line 27 I'm assuming this is (X-m)^2
Something like that
Arnoldjavs3 said:
Line 29 Determines the mean of line 27
Line 30 Determines the standard deviation

lines 31-33 relays the information to the reader.
Yes
Arnoldjavs3 said:
1 question I have is how would one be able to organize the relayed information into a table?
You'll need to use printf() and format your output in columns.

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)

For that line of code, change this:
Code:
printf("Standard Deviation = SD = sqrt(%.2f)\n",sqrt(Variance));
to:
Code:
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.

Arnoldjavs3
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)

## 1. What is the purpose of the first assignment in a C Programming course?

The first assignment in a C Programming course is designed to introduce students to the basic concepts and syntax of the C programming language. It also helps students to develop problem-solving skills and familiarize themselves with common programming tasks such as input/output, loops, and conditional statements.

## 2. How should I approach the first assignment if I have no prior experience in programming?

If you have no prior experience in programming, it is important to start by understanding the basic principles of programming, such as variables, data types, and control structures. It is also helpful to read through the instructions for the assignment carefully and break down the task into smaller, manageable steps. You can also seek assistance from your instructor or classmates if you encounter any difficulties.

## 3. Is it necessary to have a deep understanding of the C language to complete the first assignment?

No, the first assignment is designed to be beginner-friendly, so you do not need to have a deep understanding of the C language to complete it. However, it is recommended to have a basic understanding of the language's syntax and concepts to complete the assignment successfully.

## 4. Can I use outside resources or help for the first assignment?

It is always best to try to complete the assignment on your own, as it will help you develop your programming skills. However, if you are struggling, you can use outside resources such as online tutorials or seek help from your instructor or classmates. Just be sure to properly cite any resources you use in your code.

## 5. How can I check if my code is correct for the first assignment?

You can use an online compiler or IDE (integrated development environment) to test your code and see if it produces the expected output. You can also ask your instructor for feedback or have a classmate review your code. It is important to thoroughly test your code to ensure it is correct and meets all the requirements of the assignment.

### Similar threads

• Engineering and Comp Sci Homework Help
Replies
6
Views
3K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
2
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
922
• Engineering and Comp Sci Homework Help
Replies
9
Views
4K
• STEM Academic Advising
Replies
12
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Engineering and Comp Sci Homework Help
Replies
6
Views
2K
• Programming and Computer Science
Replies
1
Views
775
• Engineering and Comp Sci Homework Help
Replies
11
Views
2K