944,022 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2785
  • C++ RSS
Feb 1st, 2005
0

I just need help starting this program please

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Feb 1st, 2005
0

Re: I just need help starting this program please

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. int X[25];
  2. int Y[25];
Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. int Z[25];
And surely you can find the product.
C++ Syntax (Toggle Plain Text)
  1. Z[i] = X[i] * Y[i];
Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. double result = sqrt(sum);
Quote ...
Prompt the user for input with N less then 25.
Surely you can prompt the user for a number.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 2nd, 2005
0

Re: I just need help starting this program please

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:
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Feb 2nd, 2005
0

Re: I just need help starting this program please

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 3rd, 2005
0

Re: I just need help starting this program please

I'm confused on how to put n into two arrays x and y prompting the user to only input n less than 25times
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Feb 4th, 2005
0

Re: I just need help starting this program please

Quote 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
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 25; ++i )
  2. {
  3. cout << "Doing something " << i + 1 << endl;
  4. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 4th, 2005
0

Re: I just need help starting this program please

This is the code that I have come up with

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Feb 4th, 2005
0

Re: I just need help starting this program please

You have one input, and it's not in a loop.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 4th, 2005
0

Re: I just need help starting this program please

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Feb 4th, 2005
0

Re: I just need help starting this program please

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:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. cout << "result = " << sqrt(sum) << endl;
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Double values with two decimal places
Next Thread in C++ Forum Timeline: What's going on with character values in this loop?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC