Here is my attempt, could you look over it and tell me what I am doing wrong?

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
void add(float[],float[],float[]);

int main()
{

float a[500000000],b[500000000],c[500000000];
for(int count =0; count<500000000;count++)
{
a[count]=rand();
b[count]=rand();
}

add(a,b,c);

return 0;
}
void add(float x[],float y[],float z[])
{
time_t begin,end;
float d=10;
float e=6;
int total=0;
for(long countk=0;countk<500000000;countk++)
{
z[countk]=0;
}

for(int j=0;j<10;j++)//test the loop ten time to get a more
accerate executing
//time
{
begin = time(NULL);//sequence of when the time begins
for(int county=0;county<500000000;county++)
{
z[county]=0;
}
for(long countu =1; countu<500000000;countu++)
{
z[countu]=x[countu]+y[countu];
end = time(NULL);//how long it took for the loop to finish
}

cout<<"time for loop is "<<end-begin<<"seconds"<<endl;
total=total+end-begin;
cout<<"total="<<total<<endl;

}//end first loop

cout<<"time for exexcuting c=a+b is"
<<500000000/(total*pow(d,6))<<endl;

}


The program should output this:

hours since January 1, 1970 first = -4196164
hours since January 1, 1970 second = 1100010441
time for the loop is 13 seconds
total = 13
hours since January 1, 1970 second = 1100010454
time for the loop is 13 seconds
total = 26
hours since January 1, 1970 second = 1100010467
time for the loop is 13 seconds
total = 39
hours since January 1, 1970 second = 1100010480
time for the loop is 13 seconds
total = 52
hours since January 1, 1970 second = 1100010493
time for the loop is 13 seconds
total = 65
hours since January 1, 1970 second = 1100010506
time for the loop is 13 seconds
total = 78
hours since January 1, 1970 second = 1100010519
time for the loop is 13 seconds
total = 91
hours since January 1, 1970 second = 1100010532
time for the loop is 13 seconds
total = 104
hours since January 1, 1970 second = 1100010545
time for the loop is 13 seconds
total = 117
hours since January 1, 1970 second = 1100010558
time for the loop is 13 seconds
total = 130
time for executing the sum of 1 to 50000

Use smaller size arrays

500000000 is 500 million or 5 x 10^9.
3*5 x 10^9 = 15 x 10^9 floats
8 bytes/float * 15 * 10^9 floats = 120 x 10^9
1gigabyte = 10^9 bytes
Therefore you would need over 120 GB of memory (if I've done my math right) to do this all at once like you are trying. How many people have that amount of memory available? It's therefore unrealistic to try to do this all at once. But since you don't need to report all the floats you use, just how long it takes to do that many operations, you could do it one at time, 500 million times, and time that. Or you could compromize between the two extremes---all at once versus one at a time, and use array sizes that actually make sense, and repeat as many times as you need to get the 500 million operations total for each mathematical operation. As one possible approach you could do this:

//declare reasonable size variables.
float a[100], b[100], c[100];
double additionTime = 0;
double subtractionTime = 0;
double divisionTime = 0;
double multiplicationTime = 0;

//follow a protocol like this:
obtain 100 random floats for a, and another 100 random floats for b.
do the four different calculations, timing each separately, accumulating times in appropriate variables.
repeat 5 x 10^2 times to see if the approach is tenable.
change appropriate variable sizes to repeat 5 X 10^7 times, recompile and complete the task.
maybe try changing size of arrays and other variables as necessary to see what happens.
experience the excitement of exploration.
go on to your next assignment.
get married and have kids.
grow old and die.

Oops. Methinks I got almost as carried away as trying to use 120 GB of memory in a single program.

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.