| | |
I just need help starting this program please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
I have to write a program to read N data items into two arrays, X and Y, of size 25.
C++ Syntax (Toggle Plain Text)
int X[25]; 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.
C++ Syntax (Toggle Plain Text)
int Z[25];
C++ Syntax (Toggle Plain Text)
Z[i] = X[i] * Y[i];
•
•
•
•
Also, compute and print the square root of the sum of the items of the three arrays.
C++ Syntax (Toggle Plain Text)
double result = sqrt(sum);
•
•
•
•
Prompt the user for input with N less then 25.
C++ Syntax (Toggle Plain Text)
int N; cout << "prompt: "; cin >> N;
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
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.
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
•
•
•
•
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)
for ( int i = 0; i < 25; ++i ) { cout << "Doing something " << i + 1 << 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
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
This is the code that I have come up with
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdlib.h> int x[25]; int y[25]; int z[25]; int n[25]; int p; int sum; void getdata(); void square(); main() { getdata(); square() } void getdata() { cout >> "please enter some numbers: "; cin << n; for(p=0;p<25;++p) { x[p] = n[p]; y[p] = n[p]; z[p]= x[p] * y[p]; } } void square() { for(p=0;p,25;++p) { sum= z[p] + x[p] + y[p]; sqrt(sum); } }
Last edited by hopeolicious; Feb 4th, 2005 at 4:49 pm. Reason: error with variables
You have one input, and it's not in a loop. How do you expect to answer 50 questions if you only ask one? (If your << and >> weren't backwards.)
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.
C++ Syntax (Toggle Plain Text)
cout >> "please enter some numbers: "; cin << n;
void square()
{
for(p=0;p,25;++p)
{
sum= z[p];
sqrt(sum);
}
}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
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
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)
#include<iostream.h> #include<stdlib.h> #include<math.h> int x[25]; int y[25]; int z[25]; int n[25]; int p; int sum; void getdata(); void square(); void main() { getdata(); square(); } void getdata() { for(p=0;p<25;++p) { cout << "Please enter some numbers: "; cin >> n[p]; x[p] = n[p]; y[p] = n[p]; z[p] = x[p] * y[p]; } } void square() { sum = 0; for(p=0;p<25;++p) { sum = z[p] + x[p] + y[p]; double result = sqrt(sum); } }
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: The main function should be declared as returning an int and actually return an int. 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. (And you can have them on separate lines.)
You will first ask for the number of loop iterations. 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. What you're doing in this loop is asking for the x and y values. You can do the calculation in the same loop, maybe adding some debugging statements. Then when the loop is complete you can calculate the final result.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; // let's go easy at first
C++ Syntax (Toggle Plain Text)
int main() { // other stuff return 0; }
C++ Syntax (Toggle Plain Text)
int x[25], y[25], z[25], i, n, sum = 0;
You will first ask for the number of loop iterations.
C++ Syntax (Toggle Plain Text)
do { cout << "How many items (no more than 25)? "; cin >> n; } while ( n < 0 || n > 25 );
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
} C++ Syntax (Toggle Plain Text)
cout << "x[" << i << "] ? "; cin >> x[i]; cout << "y[" << i << "] ? "; cin >> y[i];
C++ Syntax (Toggle Plain Text)
z[i] = x[i] * y[i]; cout << "z[" << i << "] = " << z[i] << '\n'; sum += z[i] + x[i] + y[i]; cout << "sum = " << sum << endl;
C++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- C:\Program Files\Common keeps opening on boot (Windows NT / 2000 / XP)
- starting program in background (C++)
- executing the normal program from linux.rc - different bahaviour (Kernels and Modules)
- Need help starting program. I don't know where to begin. (Pascal and Delphi)
- Need help starting program using vector (C++)
Other Threads in the C++ Forum
- Previous Thread: reading txt file into array
- Next Thread: What's going on with character values in this loop?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






