Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > Other Topics > Computer Technical Help
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2006, 10:49 AM
RedJoker RedJoker is offline
Senior Member
 
Join Date: Aug 2006
Location: High on Life
Posts: 2,353
Default 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.
Reply With Quote
  #2  
Old 10-30-2006, 06:23 PM
TheRock69 TheRock69 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 291
Default 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;
}
Reply With Quote
  #3  
Old 10-30-2006, 06:56 PM
RedJoker RedJoker is offline
Senior Member
 
Join Date: Aug 2006
Location: High on Life
Posts: 2,353
Default 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.
Reply With Quote
  #4  
Old 10-30-2006, 11:32 PM
Brice Brice is offline
Senior Member
 
Join Date: May 2005
Location: The Rock
Posts: 616
Default 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.
Reply With Quote
  #5  
Old 10-31-2006, 01:38 AM
Lurker4 Lurker4 is offline
Senior Member
 
Join Date: Oct 2004
Location: Urbana, IL
Posts: 762
Default 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.
Reply With Quote
  #6  
Old 10-31-2006, 02:15 AM
RocketManJames RocketManJames is offline
Senior Member
 
Join Date: Nov 2002
Posts: 1,033
Default 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
Reply With Quote
  #7  
Old 10-31-2006, 10:00 AM
RedJoker RedJoker is offline
Senior Member
 
Join Date: Aug 2006
Location: High on Life
Posts: 2,353
Default 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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.