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 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.

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.

Surely you know the declaration.

int Z[25];

And surely you can find the product.

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.

double result = sqrt(sum);

Prompt the user for input with N less then 25.

Surely you can prompt the user for a number.

int N;
cout << "prompt: ";
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.

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:

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.

I'm confused on how to put n into two arrays x and y prompting the user to only input n less than 25times

I'm confused on how to put n into two arrays x and y prompting the user to only input n less than 25times

for ( int i = 0; i < 25; ++i )
   {
      cout << "Doing something " << i + 1 << endl;
   }

This is the code that I have come up with

#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);
    }
}

You have one input, and it's not in a loop.

cout >> "please enter some numbers: ";
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.

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

#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:

#include <iostream>
#include <cmath>
using namespace std; // let's go easy at first

The main function should be declared as returning an int and actually return an int.

int main()
{
   // other stuff
   return 0;
}

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.

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.

do
   {
      cout << "How many items (no more than 25)? ";
      cin  >> n;
   } 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.

cout << "x[" << i << "] ? ";
      cin  >> x[i];

      cout << "y[" << i << "] ? ";
      cin  >> y[i];

You can do the calculation in the same loop, maybe adding some debugging statements.

z[i] = x[i] * y[i];
      cout << "z[" << i << "] = " << z[i] << '\n';

      sum += z[i] + x[i] + y[i];
      cout << "sum = " << sum << endl;

Then when the loop is complete you can calculate the final result.

cout << "result = " << sqrt(sum) << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.