#1
|
|||
|
|||
C Programming Noob - Need Help Plz.
I have an assignment but don't have a clue what to do. It's a maths course so the problems are all maths related. Any help greatly appreciated. Here's the first question.
1. Write a program that reads from keyboard an ‘int’ number n, then reads n ‘double’ numbers xi and prints their sum and their product (summation)from i=1 to n of xi, (product) from i=1 to n of xi The program should check that all values are valid. The commands we've learned so far are printf, scanf, if, for, while and else. There might be a few more but I'm not sure. The code should start off like this: # include <stdio.h> # include <math.h> void main(){ } I would probably start off with printf("Please enter a list of integers: ") After that I'm stuck. Thanks for any help you can give me. |
#2
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
I can give it to you in c++. This is pretty basic programming you should really learn it.
#include <iostream> using namespace std; int main() { int num1; double num2; double sum; double product; cout << "Enter an int: "; cin >> num1; cout << "Enter a double (decimal): "; cin >> num2; product = num1 * num2; sum = num1 + num2; cout << "Sum of the nums is: " << sum << endl; cout << "Product of the nums is: " << product << endl; return 0; } |
#3
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
Thank you so much Rock.
I'm trying to learn this stuff, I bought C++ for dummies and am reading that at the moment. This is the first time I've ever done programming so it's all completely new to me. I'm in second year at the moment and C programming was an optional class in first year, only half the class didn't know. So the lecturer is asking 'advanced' questions to the class and getting answers from ppl who did it last year. Meanwhile the rest of us are struggling with the basics. Here's the next question, thanks again for any help. 2. Write a program that reads floating-point numbers from keyboard until the user enters 0, and prints their arithmetic mean and geometric mean 1/n [(summation)from i=1 to n of xi], [(product) from i=1 to n of xi]^1/n where n is the number of values read, excluding the final 0. The program should check that all values are valid. |
#4
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
Why don;t you give us some code and we can point you from there? Your never going to learn this stuff unless you do it yourself.
|
#5
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
[ QUOTE ]
I can give it to you in c++. This is pretty basic programming you should really learn it. #include <iostream> using namespace std; int main() { int num1; double num2; double sum; double product; cout << "Enter an int: "; cin >> num1; cout << "Enter a double (decimal): "; cin >> num2; product = num1 * num2; sum = num1 + num2; cout << "Sum of the nums is: " << sum << endl; cout << "Product of the nums is: " << product << endl; return 0; } [/ QUOTE ] This just prints the sum and product of two numbers, not the sequence of numbers that he needs. This should be the answer in C, although there might be a few syntax errors: int n; double product = 1; double sum = 0; double a; printf("Enter int n:"); scanf("%d", &n); printf("\n"); for (int i = 1; i <= n; i++) { printf("Enter double %d:", i) scanf("%f", &a); printf("/n"); sum = sum + a; product = product * a; } printf("Sum is: %d\n", sum); printf("Product is: %d\n", product); I don't want to sound like a jerk but this is really basic stuff. If you're going to do more C/C++ programming in this class, I'd either carefully go through the examples in your book, or go through some online tutorial. Also, if you understand the above code, then the next question is similar but using a while loop and an if statement. |
#6
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
[ QUOTE ]
for (int i = 1; i <= n; i++) { printf("Enter double %d:", i) scanf("%f", &a); printf("/n"); sum = sum + a; product = product * a; } ... double product = 1; double sum = 0; printf("Sum is: %d\n", sum); printf("Product is: %d\n", product); [/ QUOTE ] I'll be a nit here... I know you said there might be syntax errors, but the following don't fit that, so here are my two quick points. In straight C, the declaration of i inside the for loop parameters is not legal. You need to declare it at the top where the other variables are being declared. Also, when you try to print out doubles with %d using printf, you're going to get some bad results. I'm pretty sure you need to use %f, or cast the variables. But, I could be wrong on this one. -RMJ |
#7
|
|||
|
|||
Re: C Programming Noob - Need Help Plz.
Thanks to everyone for all your help.
The following code is just adapting Lurker's original code. I know this is probably messy and illogical but I'll post it anyway and we can go from there. # include <stdio.h> # include <math.h> void main() float n; double product = 1; double sum = 0; double geommean = 1; double arithmean = 0; double a; printf("Please enter a float n: "); scanf("%d", &n); printf("\n"); for (int i = 1; i<= n; i++) { printf("Enter double %d:", i) scanf("%f", &a); printf("/n"); sum = sum + a; product = product * a; if(i == 0)break; arithmean = sum/n; geommean = product^(1/n); } printf("arithmetic mean is: %d\n", arithmean) printf("geometric mean is: %d\n", geommean) I know this is probably very basic stuff but it's the first time I've done any type of programming. At the moment it's like trying to understand japanese. I probably just need more exposure to it. If you could point out the mistakes in the code that would be great (plz be gentle). Thanks again. |
|
|