c++ - Need help parsing a convoluted file - c++

[Setup]
There are three data files (csv – space delimited).
Each file, and each line in that file, represents a data that will be used.
Line 1 of each file makes subset_1, Line 2 of each file makes subset_2 and so on until you have four subsets. Then you have a full data set consisting of lines 1~4 in each file.
Since there are 16 lines in each file and each data set consists of 4 lines.... there is a total of four datasets to be dealt with.
What happens now is that each line of data for each file is run through the parser.
Line 1 (all three files) – subset_1
The percentage difference between multiple points is calculated.
The percentage difference generated from step one is turned into an absolute value
The non-absolute value, from step one, is set as the non-absolute subtotal
The absolute value, from step two, is set as the absolute subtotal.
Line 2 (all three files) – subset_2
The percentage difference between multiple points is calculated.
The percentage difference generated from step one is turned into an absolute value
The non-absolute value, from step one, is added to the non-absolute subtotal
The absolute value, from step two, is added to the absolute subtotal.
…
The same thing is done for lines 3 and 4. After line four you have dataset 1 non-absolute/absolute totals
…
Line 5 (all three files) – subset_5
repeat all the same steps you did previously for subsets_1~4
…
Repeat again and again until all sixteen lines have been read and four datasets have been created.
At present all of the above steps are working fine.
[What I am trying to do]
For each full data set that is created I want to output this dataset to an array so that I can use this data in more math/calculations. I have tried and succeeded at putting the data into an array, however, getting the data out of an array is not so easy because of one reason – a decimal point. Column 1 and column 2 of each data file contains a number with a decimal point. I cannot figure out how to get the data out of the array that I put it into because I don't know how :(
Can someone help me get this working?
XXX WORKING FILES XXX
Statistics_BOB_1400MHz.csv _http://pastebin.com/9xXZDSxQ
Statistics_BOB_1700MHz.csv _http://pastebin.com/a6yGcv6Z
Statistics_BOB_2300MHz.csv _http://pastebin.com/tnGKwAyB
XXXXXXXXXXXXXXXXXXXXX
/*
* main.cpp
*
* Created on: Jul 30, 2015
* Author: youngc
*/
#include <iostream>
#include <string>
#include <sstream>
#include <cerrno>
#include <errno.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
//step info & names for the raw data array
struct stepData
{
// The following letters correspond to the Columns in the spread sheet
// "" 0 <-- Does not exist
double VEUP; // A 1
double VEDOWN; // B 2
string info; // C 3
int group; // D 4
int step; // E 5
double pwr;//measured chip power from Arduino // F 6
double calcDynPwr; // G 7
double calcChipPwr; // H 8
double estDynErr; // I 9
double estChipPwr; // J 10
double estDynErrAbs; // K 11
double estChipErrAbs; // L 12
string staP5PH; // Place Holder // M 13
double staP5; // N 14
string dynP5PH; // Place Holder // O 15
double dynP5; // P 16
string chipP5PH; // Place Holder // Q 17
double chipP5; // R 18
string staP4PH; // Place Holder // S 19
double staP4; // T 20
string dynP4PH; // Place Holder // U 21
double dynP4; // V 22
string chipP4PH; // Place Holder // W 23
double chipP4; // X 24
string staP3PH; // Place Holder // Y 25
double staP3; // Z 26
string dynP3PH; // Place Holder // AA 27
double dynP3; // AB 28
string chipP3PH; // Place Holder // AC 29
double chipP3; // AD 30
string staP2PH; // Place Holder // AE 31
double staP2; // AF 32
string dynP2PH;// Place Holder // AG 33
double dynP2; // AH 34
string chipP2PH; // Place Holder // AI 35
double chipP2; // AJ 36
string staP1PH; // Place Holder // AK 37
double staP1; // AL 38
string dynP1PH; // Place Holder // AM 39
double dynP1; // AN 40
string chipP1PH; // Place Holder // AO 41
double chipP1; // AP 42
double chipEnergyMeasure; // AQ 43
double chipEnergy5; // AR 44
double chipEnergy4; // AS 45
double chipEnergy3; // AT 46
double chipEnergy2; // AU 47
double chipEnergy1; // AV 48
double junk1; // AW 49
double junk2; // AX 50
};
// There are 48 variables
// Split the line up into an array, 0 - 50 variables starting at 0, Array Size = 51
stepData columnData1, columnData2, columnData3;
// String variable names
string fileData1, fileData2, fileData3;
// Needed for the file.open
ifstream inputFile1, inputFile2, inputFile3;
int main ()
{
// variable name for the number of rows in a spread sheet
int numLines;
int numLines1=0, numLines2=0, numLines3=0;
int rowsCount=0, dataSetCount=0;
int i, j;
// Open one of the files to get the number of lines that you will be working with
inputFile1.open("Statistics_BOB_1400MHz.csv");
inputFile2.open("Statistics_BOB_1700MHz.csv");
inputFile3.open("Statistics_BOB_2300MHz.csv");
// Make sure that all of the input files have the same number of lines
if ( inputFile1.is_open() )
{
//cout << "Opening the " << inputFile_1 << "file \n";
while (getline (inputFile1, fileData1))
{
numLines1++;
}
}
// Close the file
inputFile1.close();
//**********
numLines = numLines1;
//**********
if ( inputFile2.is_open() )
{
//cout << "Opening the " << inputFile_2 << "file \n";
while (getline (inputFile2, fileData2))
{
numLines2++;
}
}
// Close the file
inputFile2.close();
if ( inputFile3.is_open() )
{
//cout << "Opening the " << inputFile_3 << "file \n";
while (getline (inputFile3, fileData3))
{
numLines3++;
}
}
// Close the file
inputFile3.close();
cout << "What is the row count for each file of input" << "\n";
cout << "*************************************************\n";
cout << "***** The rowCount_1 is " << numLines1 << " *****\n";
cout << "***** The rowCount_2 is " << numLines2 << " *****\n";
cout << "***** The rowCount_3 is " << numLines3 << " *****\n";
cout << "*************************************************\n\n";
//cout << "Start int main()" << "\n";
// Working variable names (data imported from each line)
double mesChip[17][11][11][4][4];
double estChip[17][11][11][4][4];
// Create array variable names
double diffChip[17][5][5][4][4]; // find the difference in power
double absDiffChip[17][5][5][4][4]; // find the absolute difference in power
double sumDiffChip[17][5][5][4][4]; // sum up the differences in power
double absSumDiffChip[17][5][5][4][4]; // sum up the absolute differences in power
// Open each results file and iterate import the data from each line of the file.
inputFile1.open("Statistics_BOB_1400MHz.csv");
inputFile2.open("Statistics_BOB_1700MHz.csv");
inputFile3.open("Statistics_BOB_2300MHz.csv");
//cout << "Start line by line import (loop)\n" << endl;
// Iterate through each line of all five files to put
// the data, of each line, into a raw data array
for (rowsCount=1; rowsCount <=16; rowsCount++)
{
cout << "\n ***** Starting rowsCount = " << rowsCount << "*****\n\n";
//DEBUG
//DEBUG
//cout << "\n ***** dataSetCount = " << dataSetCount << "*****\n\n";
//cout << "import file 1" << "\n";
// Iterate through the file
if ( inputFile1.is_open() )
{
inputFile1 >>
//columnData1 <<-- does not exist
columnData1.VEUP >>
columnData1.VEDOWN >>
columnData1.info >>
columnData1.group >>
columnData1.step >>
columnData1.pwr >>
columnData1.calcDynPwr >>
columnData1.calcChipPwr >>
columnData1.estDynErr >>
columnData1.estChipPwr >>
columnData1.estDynErrAbs >>
columnData1.estChipErrAbs >>
columnData1.staP5PH >>
columnData1.staP5 >>
columnData1.dynP5PH >>
columnData1.dynP5 >>
columnData1.chipP5PH >>
columnData1.chipP5 >>
columnData1.staP4PH >>
columnData1.staP4 >>
columnData1.dynP4PH >>
columnData1.dynP4 >>
columnData1.chipP4PH >>
columnData1.chipP4 >>
columnData1.staP3PH >>
columnData1.staP3 >>
columnData1.dynP3PH >>
columnData1.dynP3 >>
columnData1.chipP3PH >>
columnData1.chipP3 >>
columnData1.staP2PH >>
columnData1.staP2 >>
columnData1.dynP2PH >>
columnData1.dynP2 >>
columnData1.chipP2PH >>
columnData1.chipP2 >>
columnData1.staP1PH >>
columnData1.staP1 >>
columnData1.dynP1PH >>
columnData1.dynP1 >>
columnData1.chipP1PH >>
columnData1.chipP1 >>
columnData1.chipEnergyMeasure >>
columnData1.chipEnergy5 >>
columnData1.chipEnergy4 >>
columnData1.chipEnergy3 >>
columnData1.chipEnergy2 >>
columnData1.chipEnergy1 >>
columnData1.junk1 >>
columnData1.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
//cout << "import file 2" << "\n";
// Iterate through the file
if ( inputFile2.is_open() )
{
inputFile2 >>
//columnData2 <<-- does not exist
columnData2.VEUP >>
columnData2.VEDOWN >>
columnData2.info >>
columnData2.group >>
columnData2.step >>
columnData2.pwr >>
columnData2.calcDynPwr >>
columnData2.calcChipPwr >>
columnData2.estDynErr >>
columnData2.estChipPwr >>
columnData2.estDynErrAbs >>
columnData2.estChipErrAbs >>
columnData2.staP5PH >>
columnData2.staP5 >>
columnData2.dynP5PH >>
columnData2.dynP5 >>
columnData2.chipP5PH >>
columnData2.chipP5 >>
columnData2.staP4PH >>
columnData2.staP4 >>
columnData2.dynP4PH >>
columnData2.dynP4 >>
columnData2.chipP4PH >>
columnData2.chipP4 >>
columnData2.staP3PH >>
columnData2.staP3 >>
columnData2.dynP3PH >>
columnData2.dynP3 >>
columnData2.chipP3PH >>
columnData2.chipP3 >>
columnData2.staP2PH >>
columnData2.staP2 >>
columnData2.dynP2PH >>
columnData2.dynP2 >>
columnData2.chipP2PH >>
columnData2.chipP2 >>
columnData2.staP1PH >>
columnData2.staP1 >>
columnData2.dynP1PH >>
columnData2.dynP1 >>
columnData2.chipP1PH >>
columnData2.chipP1 >>
columnData2.chipEnergyMeasure >>
columnData2.chipEnergy5 >>
columnData2.chipEnergy4 >>
columnData2.chipEnergy3 >>
columnData2.chipEnergy2 >>
columnData2.chipEnergy1 >>
columnData2.junk1 >>
columnData2.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
//cout << "import file 3" << "\n";
// Iterate through the file
if ( inputFile3.is_open() )
{
inputFile3 >>
//columnData3 <<-- does not exist
columnData3.VEUP >>
columnData3.VEDOWN >>
columnData3.info >>
columnData3.group >>
columnData3.step >>
columnData3.pwr >>
columnData3.calcDynPwr >>
columnData3.calcChipPwr >>
columnData3.estDynErr >>
columnData3.estChipPwr >>
columnData3.estDynErrAbs >>
columnData3.estChipErrAbs >>
columnData3.staP5PH >>
columnData3.staP5 >>
columnData3.dynP5PH >>
columnData3.dynP5 >>
columnData3.chipP5PH >>
columnData3.chipP5 >>
columnData3.staP4PH >>
columnData3.staP4 >>
columnData3.dynP4PH >>
columnData3.dynP4 >>
columnData3.chipP4PH >>
columnData3.chipP4 >>
columnData3.staP3PH >>
columnData3.staP3 >>
columnData3.dynP3PH >>
columnData3.dynP3 >>
columnData3.chipP3PH >>
columnData3.chipP3 >>
columnData3.staP2PH >>
columnData3.staP2 >>
columnData3.dynP2PH >>
columnData3.dynP2 >>
columnData3.chipP2PH >>
columnData3.chipP2 >>
columnData3.staP1PH >>
columnData3.staP1 >>
columnData3.dynP1PH >>
columnData3.dynP1 >>
columnData3.chipP1PH >>
columnData3.chipP1 >>
columnData3.chipEnergyMeasure >>
columnData3.chipEnergy5 >>
columnData3.chipEnergy4 >>
columnData3.chipEnergy3 >>
columnData3.chipEnergy2 >>
columnData3.chipEnergy1 >>
columnData3.junk1 >>
columnData3.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
cout << "columnData1.VEUP = " << columnData1.VEUP << "\n";
cout << "((columnData1.VEUP * 10.)- 10 = " << ((columnData1.VEUP * 10.)-10);
int VEUPLOOP = (int)(columnData1.VEUP * 10.) - 10;
cout << "VEUPLOOP = " << VEUPLOOP << "\n";
cout << "columnData1.VEDOWN = " << columnData1.VEDOWN << "\n";
cout << "((columnData1.VEUP * 10.)- 10 = " << ((columnData1.VEUP * 10.)-10);
int VEDOWNLOOP = (int)(columnData1.VEDOWN * 10.) - 10;
cout << "VEDOWNLOOP = " << VEDOWNLOOP << "\n";
//cout << "\n\nStart data calculations, VEUPLOOP, VEDOWNLOOP" << "\n\n";
// retrieve the relevant line data from the array
// -- Arduino measured power
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.calcChipPwr;
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] = columnData2.calcChipPwr;
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] = columnData3.calcChipPwr;
// -- projected power data
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][1] = columnData2.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][1] = columnData3.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][2] = columnData1.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] = columnData2.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][2] = columnData3.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][3] = columnData1.chipP3;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][3] = columnData2.chipP3;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] = columnData3.chipP3;
cout << "\nFrequency 1" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][1] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][1] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][1] << "\n";
cout << "\nFrequency 2" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][2] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][2] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][2] << "\n";
cout << "\nFrequency 3" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][3] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][3] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][3] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] << "\n\n";
// Calculate the difference in power for the measurements
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] = ((estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] - mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i])/mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i]);
//diffDataStorage[dataPoint][loop][VEUPLOOP][VEDOWNLOOP][j][i] = diffChip[VEUPLOOP][VEDOWNLOOP][j][i];
cout << "diffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << \
((estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] - \
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i])/mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i]) << \
"\t>>>> " << \
diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << \
" =(" << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << \
" - " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i] << ") / " <<\
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i] << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
absDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] = fabs(diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i]);
cout << "absDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << \
fabs(diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i]) << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
sumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] += diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i];
cout << "sumDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << sumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
cout << "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
absSumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] += absDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i];
//int remainder = rowsCount % 52;
//if (remainder == 0)
//{
cout << "absSumDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << absSumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << "\n";
//}
}
// end of for j diffChip
}
// end of for i diffChip
cout << "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << "\n";
//add a line spacer
cout << "\n";
// end of dataSetCount
//DEBUG
}
//end of if (rowsCount
inputFile1.close();
inputFile2.close();
inputFile3.close();
return 0;
}

Stop thinking of this as one problem. There are a minimum of three problems here (read in data, process data, output results) so start by breaking the program up with functions.
For example, with a slightly smarter stepData that knows how to read itself from a file:
struct stepData
{
double VEUP; // A 1
double VEDOWN; // B 2
...
double junk2; // AX 50
bool readin(ifstream &inputFile)
{
std::string line;
if (std::getline(inputFile, line)) // read in exactly one line
{
std::stringstream stream(line);// parse the line
if (stream >>
//columnData1 <<-- does not exist
VEUP >>
VEDOWN >>
...
junk2)
{
return true;
}
}
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return false;
}
};
The previous version would read off the end of a line and into the next if the file was malformed. It also did not check any of the reads for success. This one does. Note that the errno is not set if chipP5 is fed "Hippo". Better error reporting is still needed, but at least you now know there was an error.
In addition that massive reading block in the middle of main condenses into
if (!(columnData1.readin(inputFile1) &&
columnData2.readin(inputFile2) &&
columnData3.readin(inputFile3)))
{
return -1;
}
If you have a bug in the input, you now have only one place to look. Not three or five or 7000. Also lost about a hundred lines of code.
You can probably do the same with most if not all of your calculation and output code.

To me this looks like a general issue of understanding indexing. In general unless you have a specific reason for doing so, you should deal with your arrays as zero-offset since that's the languages native representation; e.g. a line like:
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.calcChipPwr;
you should prefer as:
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][0][0] = columnData1.calcChipPwr;
Second, your issue of the "floating point" index. When you are calculating VEUPLOOP and VEDOWNLOOP you transform that aspect of the value away:
int VEUPLOOP = (int)(columnData1.VEUP * 10.) - 10;
so 1 becomes 0 and 1.1 becomes 1. This points out another problem with your code -- all of your data files include VEUP values of 2, which results in a VEUPLOOP value of 10. Not all of your data structures are dimensioned to have valid storage at that index location, so I'd expect to see possibly garbaged values (or a crash) in calculations involving any of your ...Diff structures.

After working on this for a while and some help from a co-worker, here a solution that works.
for (int x = 0; x <=rowsCount; x +=dataGroupSize)
{
double sum_err[6][6];
// set(reset) the error to zero
double grp_err = 0.;
// set the array contents to zero
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err[i][j] = 0.;
}
}
//
for (int y = 0; y < dataGroupSize; y++)
{
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err[i][j] += diffChip[x+y][i][j];
}
}
}
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
grp_err += (sum_err[i][j]/dataGroupSize);
if (i ==testFreqs && j==testFreqs)
{
avgErrFile << veup_array[x] << "," << vedown_array[x] << ",grp_err," << (grp_err/25) << "\n";
}
}
}
}
// Calculate absolute data
for (int x = 1; x <=rowsCount; x +=dataGroupSize)
{
double sum_err_abs[6][6];
double grp_err_abs = 0.;
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err_abs[i][j] = 0.;
}
}
for (int y = 0; y < dataGroupSize; y++)
{
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err_abs[i][j] += absDiffChip[x+y][i][j];
}
}
}
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
grp_err_abs += (sum_err_abs[i][j]/dataGroupSize);
if (i ==testFreqs && j==testFreqs)
{
absAvgErrFile << veup_array[x] << "," << vedown_array[x] << ",grp_err_abs," << (grp_err_abs/25) << "\n";
}
}
}

Related

How is this BTC Code validating the user input?

I have this CODE to take user input and validate it to increase the user BTC in User wallet.
I need some Explanation of this CODE how it works.
The Code:
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation, btc_validation_1_input, btc_validation_2_input;
result_btc = 0;
btc_validation = 0;
btc_validation_1_input = 0;
btc_validation_2_input = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
btc_validation_1_input == btc_validation + btc_input;
cout << "Enter the Value " << (btc_validation_1_input + 1) << " + " ;
cin >> btc_validation_2_input;
result_btc = btc_validation_1_input + btc_validation_2_input;
result_btc *= btc_validation_2_input;
break;
}
cout << result_btc << '\n';
cout << "Your BTC Wallet is Increased by " << result_btc << " coins " << '\n';
}
Your Code is not an API in any case but a code, and be aware with scammers. The code you have is working the same way as I will explain by editing and converting your code in a minimum form for your better understanding.
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation;
result_btc = 0;
btc_validation = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the number >> [" << btc_validation + 1 << "] " ;
cin >> btc_input;
result_btc += btc_input;
}
cout << (result_btc *= btc_input) << '\n';
cout << "You entered " << result_btc << " numbers " << '\n';
}

Football tournament with matrix

I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```

Issue with code P: PP Drill 4

I've been doing a drill from a book entitled Programming: Principles and Practice Using C++ by Bjarne Stroustrup, and this drill (Chapter 4) primarily deals with vectors. When I run the program, it doesn't give me any output at all. There are no compilation errors though. Here's the code:
#include "std_lib_facilities.h"
int main()
{
vector<double> value;
vector<string> units;
double max_val=-100000, min_val=100000;
double temp, sum=0;
int no_of_inputs=0;
string unit;
int i=0;
cout << "\nEnter the first value: " << endl;
// inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
// converts cm, in and ft to m;
for (i==0; i<units.size(); ++i){
if (units[i]== "cm" || units[i]== " cm"){
value[i] = value[i]/100.0;
}else if (units[i]== "in" || units[i]== " in"){
value[i] = value[i]/2.5/100.0;
}else if (units[i]== "ft" || units[i]== " ft"){
value[i] = value[i]*12/2.5/100.0;
}else if (units[i]=="m" || units[i]== " m"){
}else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
// Identifies the max_value and the min_value. Also adds all values.
for (i==0; i<value.size(); ++i){
if (value[i]>max_val){
max_val=value[i];
}
if (value[i]<min_val){
min_val=value[i];
}
sum += value[i];
}
// outputs all values entered (converted to meters)
cout << "\nValues Entered:"<< endl;
sort(value);
for (i==0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;
}
Can anyone tell me why it is not working?
you have 2 mistakes. The main reason for terminating your program is here
else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value."; return 0;
you putting return in the first for loop. I guess you may think return is belong to else but because you don't use scope determiner {} only 1 instruction (cout) is belong to else and return is executed every time.
your second mistake is use i==0 instead of i=0 in for loops.
your correct code is this :
vector<double> value;
vector<string> units;
double max_val = -100000, min_val = 100000;
double temp, sum = 0;
int no_of_inputs = 0;
string unit;
int i = 0;
cout << "\nEnter the first value: " << endl;
//inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
//converts cm, in and ft to m;
for (i = 0; i<units.size(); ++i){
if (units[i] == "cm" || units[i] == " cm"){
value[i] = value[i] / 100.0;
}
else if (units[i] == "in" || units[i] == " in"){
value[i] = value[i] / 2.5 / 100.0;
}
else if (units[i] == "ft" || units[i] == " ft"){
value[i] = value[i] * 12 / 2.5 / 100.0;
}
else if (units[i] == "m" || units[i] == " m")
{
}
else
{
cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
}
//Identifies the max_value and the min_value. Also adds all values.
for (i = 0; i<value.size(); ++i){
if (value[i]>max_val){
max_val = value[i];
}
if (value[i]<min_val){
min_val = value[i];
}
sum += value[i];
}
//outputs all values entered (converted to meters)
cout << "\nValues Entered:" << endl;
//sort(value);
for (i = 0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;

Setting up precision C++

So, i'm attempting to set the precision for input values in my code. I want the values to be printed with two decimal points afterwards of precision though i'm not exactly sure how.
Here's my code.
#include <iostream>
#include <iomanip>
float uphill, wellD, waterLvl, buckVol;
float buckAscRate, downHill, volume;
float last;
float timeReq;
int scene = 1;
void timeRequired()
{
std::setw(2);
std::setprecision(2);
std::cout << "Scenario " << scene << ":" << std::endl;
std::cout << "up hill" << " " << uphill << " sec" << std::endl;
std::cout << "well diamter" << " " << wellD << " in" << std::endl;
std::cout << "water level" << " " << waterLvl << " in" << std::endl;
std::cout << "bucket volume" << " " << buckVol << " cu ft" << std::endl;
std::cout << "bucket ascent rate" << " " << buckAscRate << " in/sec" << std::endl;
std::cout << "down hill" << " " << downHill << " sec" << std::endl;
std::cout << "required volume" << " " << volume << " cu ft" << std::endl;
timeReq = (uphill + downHill);
std::cout << "TIME REQUIRED" << " " << timeReq << " sec" << std::endl;
std::cout << " " << std::endl;
}
void scenarioCONT()
{
do
{
std::cin >> wellD;
std::cin >> waterLvl;
std::cin >> buckVol;
std::cin >> buckAscRate;
std::cin >> downHill;
std::cin >> volume;
std::cin >> last;
if (uphill <= 1) uphill = 2;
if (wellD <= 0) wellD = 1;
if (waterLvl <= 0) waterLvl = 1;
if (buckVol <= 0) buckVol = 1;
if (buckAscRate <= 0) buckAscRate = 1;
if (downHill <= 0) buckAscRate = 1;
if (volume <= 0) volume = 1;
if (last > 1)
{
uphill = last;
scenarioCONT();
}
} while (last != 0);
}
void scenario()
{
do
{
std::cin >> uphill;
std::cin >> wellD;
std::cin >> waterLvl;
std::cin >> buckVol;
std::cin >> buckAscRate;
std::cin >> downHill;
std::cin >> volume;
std::cin >> last;
if (uphill <= 1) uphill = 2;
if (wellD <= 0) wellD = 1;
if (waterLvl <= 0) waterLvl = 1;
if (buckVol <= 0) buckVol = 1;
if (buckAscRate <= 0) buckAscRate = 1;
if (downHill <= 0) buckAscRate = 1;
if (volume <= 0) volume = 1;
if (last > 1)
{
timeRequired();
uphill = last;
scenarioCONT();
}
scene++;
timeRequired();
} while (last != 0);
}
int main()
{
scenario();
system("pause");
}
I've been told to use ionmanip to set the precision, though i'm not 100% on how to do it. Any suggestions?
You can use std::setprecision function. The below example is directly taken from http://www.cplusplus.com/reference/iomanip/setprecision/
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
If you want to output 2 decimal digits, you should use std::fixed, together with std::setprecision().
Take a look here.
For easier understanding, here is an example:
cout << setprecision(2)<< 3.1415; outputs 3.1 (2 digits in total) and
cout << setprecision(2)<<fixed<< 3.1415; outputs 3.14 (2 digits after floating point)

c++ while loop from file with different data types

I have a file that includes this information:
Bev Powers
3
76 81 73
Chris Buroughs
5
88 90 79 81 84
Brent Mylus
2
79 81
I have a count controlled loop that will do the first 3 lines and use the information correctly but I am struggling with a loop that will reuse the the loop until all the information is displayed from the file regardless of how many golfers with matches are on the file. I am asking for pointers in the right direction, any assistance would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream inScores;
string filename;
string name;
int loopCount, matchScore;
int count = 1;
float mean = 0;
float adder = 0;
int main()
{
cout << endl << "Enter the golfer's filename: ";
getline(cin,filename);
cout << endl;
inScores.open(filename.c_str());
if(!inScores)
{
cout << "** " << filename << " does not exist. Please ";
cout << "check the spelling and rerun ";
cout << "the program with an existing golfer file. ** " << endl << endl;
return 1;
}
getline(inScores,name);
inScores >> loopCount;
cout << name << " has " << loopCount << " matches with scores of" << endl << endl;
inScores >> matchScore;
while (count <= loopCount)
{
cout << "Match " << count << ": " << matchScore << endl;
adder = adder + matchScore;
adder = adder + matchScore;
inScores >> matchScore;
count++;
}
cout << endl;
int(mean) = .5 + (adder / loopCount);
cout << "The mean score is " << mean << endl << endl;
inScores.close();
return 0;
}
As stated using loops will be necessary to get what you want. Also since the getline and extraction return false if they fail you can use them for the test in the loop:
ifstream inScores;
string filename;
string name;
int loopCount , matchScore;
int count = 1;
float mean = 0;
float adder = 0;
int main()
{
cout << endl << "Enter the golfer's filename: ";
getline( cin , filename );
cout << '\n';
inScores.open( filename );
if ( !inScores )
{
cout << "** " << filename << " does not exist. Please ";
cout << "check the spelling and rerun ";
cout << "the program with an existing golfer file. ** " << "\n\n";
return 1;
}
while ( getline( inScores , name ) )
{
if ( inScores >> loopCount )
{
cout << name << " has " << loopCount << " matches with scores of" << "\n\n";
}
else
{
cout << "File read error";
return 1;
}
for ( int count = 1; count <= loopCount; count++ )
{
if ( inScores >> matchScore )
{
cout << "Match " << count << ": " << matchScore << '\n';
adder = adder + matchScore;
}
else
{
cout << "File read error";
return 1;
}
}
}
cout << '\n';
int( mean ) = .5 + ( adder / loopCount );
cout << "The mean score is " << mean << "\n\n";
inScores.close();
return 0;
}