Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 2D Array Help I am having trouble with a C++ 2d array. Will rate all helpful

ID: 3880870 • Letter: C

Question

C++ 2D Array Help

I am having trouble with a C++ 2d array. Will rate all helpful solution thumbs up. Thank you!

Question:

Assumption:

- There is at least one value in the file, no more than 10 values in one line, and no more than 10 lines.

Requirements:

Example output:

My code (menu is functional):

#include <iostream>

#include <fstream>

#define SIZE 10

using namespace std;

void printarray();

void readFile();

void incrementOne();

void decrementOne();

void addValue();

int main(int argc, char *argv[])

{

ifstream inf(argv[1]);

double data[SIZE][SIZE];

int option = 0;

bool loop = true;

readFile();

while (loop = true)

{

cout << "1, Increment all values by 1" << endl;

cout << "2. Decrement all values by 1" << endl;

cout << "3. Add value to entire array" << endl;

cout << "4. Exit" << endl;

cout << "Select an option: ";

cin >> option;

  

switch (option)

{

case 1: //Incerement all values by 1

incrementOne();

printarray();

break;

case 2: //Decrement all values by 1

decrementOne();

printarray();

break;

case 3: //Add value to entire array

addValue();

printarray();

break;

case 4: //Exit

return 0;

}

}

return 0;

}

void readFile()

{

}

void printarray()

{

}

void incrementOne()

{

}

void decrementOne()

{

}

void addValue()

{

}

Suppose floating-point values exists in a file as such 3.3 1.1 8.22 6.45 2.1 3 5 7 We would like to read these values into a two-dimensional array and manipulate them in the following ways: 1) Increment each value by 1 2) Decrement each value by 1 3) Add a specific floating-point value to each value

Explanation / Answer

There are many different ways of reading this file. The getline() will require the use of a stringstream to process the string then you can use the extraction operator to extract the numbers from this string stream since the extraction operator will stop processing when it encounters the comma, then you will need to also extract and discard the comma.

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>


#define SIZE 10

using namespace std;

vector< vector<string> > array;

void readFile()
{
using namespace std;

ifstream in("your_file.txt");

string line, field;

// the 2D array
vector<string> v; // array of values for one line only

while ( getline(in,line) ) // get next line in file
{
v.clear();
stringstream ss(line);

while (getline(ss,field,',')) // break line into comma delimitted fields
{
v.push_back(field); // add each field to the 1D array
}

array.push_back(v); // add the 1D array to the 2D array
}


}
//Print the values
void printarray()
{
// print out what was read in

for (size_t i=0; i<array.size(); ++i)
{
for (size_t j=0; j<array[i].size(); ++j)
{
cout << array[i][j] << "|"; // (separate fields by |)
}
cout << " ";
}
}

//Increment by 1
void incrementOne()
{
for (size_t i=0; i<array.size(); ++i)
{
for (size_t j=0; j<array[i].size(); ++j)
{
array[i][j] =array[i][j]+1; // (separate fields by |)
}
  
}
}
//Decrement by one
void decrementOne()
{
for (size_t i=0; i<array.size(); ++i)
{
for (size_t j=0; j<array[i].size(); ++j)
{
array[i][j] =array[i][j]-1; // (separate fields by |)
}
  
}
}
//Add value to array
void decrementOne(int value)
{
for (size_t i=0; i<array.size(); ++i)
{
for (size_t j=0; j<array[i].size(); ++j)
{
array[i][j] =array[i][j]-value; // (separate fields by |)
}
  
}
}

int main(int argc, char *argv[])

{

ifstream inf(argv[1]);

double data[SIZE][SIZE];

int option = 0;

bool loop = true;

readFile();

while (loop = true)

{

cout << "1, Increment all values by 1" << endl;

cout << "2. Decrement all values by 1" << endl;

cout << "3. Add value to entire array" << endl;

cout << "4. Exit" << endl;

cout << "Select an option: ";

cin >> option;

  

switch (option)

{

case 1: //Incerement all values by 1   

incrementOne();

printarray();

break;

case 2: //Decrement all values by 1

decrementOne();

printarray();

break;

case 3: //Add value to entire array

addValue();

printarray();

break;

case 4: //Exit

return 0;

}

}

return 0;

}