Here is what I currently have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char filename[80]="Proj7data.txt";//filename is defined within code
ifstream infile; //Defines the input file stream object
infile.open(filename); //opens the input file
/*
The file is set up as follows;
The first element of the file gives the number of data sets in the file (int)
Then each set to be processed is listed sequentially in the file:
The number of elements in each array (int)
followed by all the elements in the first array (real values)
then followed by all the elements in the second array (real values)
*/
int num_datasets, num_elements;
infile>>num_datasets;//infiles first dataset which tells how many sets of data there are
for (int i = 0 ; i < num_datasets ; i++)//use i to start counter
{
float elements1[50], elements2[50], dot_product;
infile>>num_elements;
cout<<"Element\tArray "<<i<<endl;
for ( int count = 0 ; count < num_elements; count++)
{
infile>>elements1[count];
cout<<(count+1)<<"\t"<<elements1[count];
cout<<endl;
}
cout<<endl;
cout<<"Element\tArray "<<(i + 1)<<endl;
for ( int count = 0 ; count < num_elements; count++)
{
infile>>elements2[count];
cout<<(count+1)<<"\t"<<elements2[count]<<"\t";
cout<<endl;
}
cout<<endl;
dot_product = 0;
for ( int count = 0 ; count < num_elements; count ++)
{
dot_product = elements1[count]*elements2[count]+ dot_product;
}
cout<<"The dot product of "<< i <<" and "<<(i+1)<<" is "<<dot_product<<endl<<endl;
}
return 0;
}
|
This is the input file I used
3 // Number of data sets
6 // number of elements in each data set
0.1
0.2
0.1
0.2
0.1
0.2
3.0
1.0
3.0
1.0
3.0
1.0
11 // number of elements in each data set
1.0
-.2
.5
.75
.9
-1.1
1.5
1.8
2.25
2.75
-3.
101.0
80.4
-20.5
-30.0
31.2
32.8
34.7
36.1
0.0
38.4
39.12
12 // number of elements in each data set
1.05
2.05
3.05
4.05
5.05
6.05
6.05
7.05
8.05
9.05
10.05
11.05
-11.05
-10.05
-9.05
-8.05
-7.05
-6.05
6.05
5.05
4.05
3.05
2.05
1.05
How might I format this as follows (numbers used are just for example)
Element Array 1 Array 2
0 1.0 0.1
1 2.0 0.2
2 3.0 0.3
3 4.0 0.4 // each number positioned under element, array 1, and array 2 respectively (post does not recognize multiple spaces)
The dot product of array 1 and 2 is ___
Element Array 3 Array 4
* * *
* * *
* * *
The dot product of array 3 and 4 is ___
etc.
Hope I'm not asking too much. I'm pretty sure I have the right idea, but I just can't figure out how to organize my nested loops to print out in above format. I'm terrible at nested loops.
Thanks,
jpc271