I just need help starting this program please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

I just need help starting this program please

 
0
  #1
Feb 1st, 2005
I have to write a program to read N data items into two arrays, X and Y, of size 25. Calculate and store the products of corresponding pairs of elements of X and Y in a third array, Z, also of size 25. Also, compute and print the square root of the sum of the items of the three arrays. Prompt the user for input with N less then 25.

I'm not sure how to start it because my mind is so confused at how its worded I dont know what to do first this is only one of three that i have to do before friday so please can you help me please I'm only asking for a start.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,399
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I just need help starting this program please

 
0
  #2
Feb 1st, 2005
I have to write a program to read N data items into two arrays, X and Y, of size 25.
Surely you can translate that into code.
  1. int X[25];
  2. int Y[25];
Calculate and store the products of corresponding pairs of elements of X and Y in a third array, Z, also of size 25.
Surely you know the declaration.
  1. int Z[25];
And surely you can find the product.
  1. Z[i] = X[i] * Y[i];
Also, compute and print the square root of the sum of the items of the three arrays.
Surely you can sum the elements of Z[] (likely in a loop) and then you can surely find the square root of this sum.
  1. double result = sqrt(sum);
Prompt the user for input with N less then 25.
Surely you can prompt the user for a number.
  1. int N;
  2. cout << "prompt: ";
  3. cin >> N;
So, what's the problem? Determining whether N is less than 25? Using a loop? Or trying to put code fragments together in an order that completes the assignment?

Take a shot at it.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: I just need help starting this program please

 
0
  #3
Feb 2nd, 2005
Thanx and to let you know getting codes together to complete the assignment wasnt the case the case was I didnt understand fully how to set up the beginning of the code because i didnt fully understand what was meant and i didnt want to go about it the wrong way :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,399
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I just need help starting this program please

 
0
  #4
Feb 2nd, 2005
Until you've done something dozens or hundreds of times, I can almost guarantee that you will do something wrong. Don't let that keep you from starting.

Showing any kind of attempt lets everyone know what you know, what you don't know, and what you think you know. You will get much more tailored responses from experienced forum regulars. And any bad habits may be headed off at the pass before you "perfect" them.

Bottom line: post your code.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: I just need help starting this program please

 
0
  #5
Feb 3rd, 2005
I'm confused on how to put n into two arrays x and y prompting the user to only input n less than 25times
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,399
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I just need help starting this program please

 
0
  #6
Feb 4th, 2005
Originally Posted by hopeolicious
I'm confused on how to put n into two arrays x and y prompting the user to only input n less than 25times
  1. for ( int i = 0; i < 25; ++i )
  2. {
  3. cout << "Doing something " << i + 1 << endl;
  4. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: I just need help starting this program please

 
0
  #7
Feb 4th, 2005
This is the code that I have come up with

  1.  
  2. #include<iostream.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6. int x[25];
  7. int y[25];
  8. int z[25];
  9. int n[25];
  10. int p;
  11. int sum;
  12.  
  13. void getdata();
  14. void square();
  15.  
  16. main()
  17. {
  18. getdata();
  19. square()
  20. }
  21.  
  22. void getdata()
  23. {
  24. cout >> "please enter some numbers: ";
  25. cin << n;
  26.  
  27. for(p=0;p<25;++p)
  28. {
  29. x[p] = n[p];
  30. y[p] = n[p];
  31. z[p]= x[p] * y[p];
  32. }
  33. }
  34.  
  35. void square()
  36. {
  37. for(p=0;p,25;++p)
  38. {
  39. sum= z[p] + x[p] + y[p];
  40. sqrt(sum);
  41. }
  42. }
Last edited by hopeolicious; Feb 4th, 2005 at 4:49 pm. Reason: error with variables
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,399
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I just need help starting this program please

 
0
  #8
Feb 4th, 2005
You have one input, and it's not in a loop.
  1. cout >> "please enter some numbers: ";
  2. cin << n;
How do you expect to answer 50 questions if you only ask one? (If your << and >> weren't backwards.)


void square()
{
for(p=0;p,25;++p)
   {
sum= z[p];
sqrt(sum);
    }
}
Typo?

Why calculate the sqrt 25 times? You only need to do it once when the loop is done calculating the sum. And you just might want to store the result rather than just discarding it.


We can work on standard headers, global variables, and other stuff after your next spin.
Last edited by Dave Sinkula; Feb 4th, 2005 at 5:04 pm. Reason: Removed some stuff after rereading the original post.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: I just need help starting this program please

 
0
  #9
Feb 4th, 2005
Lol I was just looking at that when I was going over it looking for my errors and I was going to change it but I guess you got to it befor me here is my corrected program

  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. int x[25];
  6. int y[25];
  7. int z[25];
  8. int n[25];
  9. int p;
  10. int sum;
  11.  
  12. void getdata();
  13. void square();
  14.  
  15. void main()
  16. {
  17. getdata();
  18. square();
  19. }
  20.  
  21. void getdata()
  22. {
  23. for(p=0;p<25;++p)
  24. {
  25. cout << "Please enter some numbers: ";
  26. cin >> n[p];
  27.  
  28. x[p] = n[p];
  29. y[p] = n[p];
  30. z[p] = x[p] * y[p];
  31. }
  32. }
  33.  
  34. void square()
  35. {
  36. sum = 0;
  37.  
  38. for(p=0;p<25;++p)
  39. {
  40. sum = z[p] + x[p] + y[p];
  41. double result = sqrt(sum);
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,399
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: I just need help starting this program please

 
0
  #10
Feb 4th, 2005
If you're programming in C++ past the year 1998, you should be using standard headers. Of course, some compilers in common use are not standard, but it really should be:
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std; // let's go easy at first
The main function should be declared as returning an int and actually return an int.
  1. int main()
  2. {
  3. // other stuff
  4. return 0;
  5. }
Your variable declarations are almost right. You only need one n, right? I changed p to i just because its a more common idiom and I'm old and set in my ways.
  1. int x[25], y[25], z[25], i, n, sum = 0;
(And you can have them on separate lines.)

You will first ask for the number of loop iterations.
  1. do
  2. {
  3. cout << "How many items (no more than 25)? ";
  4. cin >> n;
  5. } while ( n < 0 || n > 25 );
Using the do ... while loop just keeps asking the question if the value of n is out of range. Note that this doesn't do much for error checking.

So then you've got a valid n, this will be the number of times you loop.
   for ( i = 0; i < n; ++i )
   {
      // yet to come
   }
What you're doing in this loop is asking for the x and y values.
  1. cout << "x[" << i << "] ? ";
  2. cin >> x[i];
  3.  
  4. cout << "y[" << i << "] ? ";
  5. cin >> y[i];
You can do the calculation in the same loop, maybe adding some debugging statements.
  1. z[i] = x[i] * y[i];
  2. cout << "z[" << i << "] = " << z[i] << '\n';
  3.  
  4. sum += z[i] + x[i] + y[i];
  5. cout << "sum = " << sum << endl;
Then when the loop is complete you can calculate the final result.
  1. cout << "result = " << sqrt(sum) << endl;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC