CSCI 152 Two Dimension Array

xoon
 (Not rated)
 (Not rated)
Chat

Write a complete program to read a data file that contains data for a two dimension array, store the data into a
two dimension array, and searches for all occurrences of a specific value in the array entered from the keyboard
by the user. The file may contain many sets of data. You will create a text file called ‘Data2d.txt’ and store as
many values as you wish. The first two values for each set of data in the file will be the number of rows and
columns to be used in the two dimension array followed by the actual numbers to be stored in the array.
/* CSCI 152 Fall 2013 Program 7 
program will read data for a two dimension array from a file
file may contain multiple data sets to process
program will enter a search value to find in the two dimension array
progran will find the locations (offset) where the search value occurred and store the location
in a single dimension array
user will continue searching for values until they want to quit
*/
#include<fstream> // header file to read data from a file
#include<iostream>
#include<iomanip> // header file to use I/O manipulators for display
using namespace std;
void read_file(short data[][15], short size, short cols, ifstream &filein);
void display_2darray(short data[][15], short rows, short cols);
void find_value(short data[][15], short rows, short cols, short locations[], short &lsize, short search_value);
void display_1darray(short data[], short size, short columnSize);
int main()
{
short data[15][15],
rows,
cols,
locations[225],
lsize,
search_value,
response;

// original data set with 15 rows and 15 columns
// number of rows of data in 2 dimension array
// number of columns of data in 2 dimension array
// array to store the locations of the search value
// number of values stored in locations arrays
// search value entered by user
// input from user to continue inner loop

ifstream filein("data2d.txt"); // input file must be in same folder with exe file
while(filein>>rows>>cols) // read number of rows and columns from file; loop terminates when file empty
{
read_file(data, rows, cols, filein);
cout<<"the array has "<<rows<<" rows and "<<cols<<endl;
display_2darray(data, rows, cols);

do // post test loop to search 2 dimension array, will execute at least 1 time
{
cout<<"enter a search value to find in the two dimension array\n)";
cin>>search_value;
find_value(data, rows, cols, locations, lsize, search_value);
if(lsize)
{
cout<<"the search value was found in "<<lsize<<" locations in the two dimension array\n";
display_1darray(locations, lsize, 15);
}
else
cout<<"the search value was not found in the array\n";
cout<<"do you want search for another value in the array? 1-yes, 0-no\n";
cin>>response;
} while(response);
}
cout<<"\nprogram done\n";
system("pause");
}

CSCI 152
Programming Fundamentals II
Program 7(two dimension array manipulation)
Searching a two dimension array
Assignment requirements continued

Fall 2013
11/6/2013

The program must contain functions to perform the following operations.
Write a function, read_file, to store values in a two dimension array obtained from a data file named –
‘Data2d.txt’. The file may contain multiple data sets. The first two values for each data set will be the
number of rows and number of columns of data for the two dimension array followed by the actual
numbers to store in the array. For example, 3, 5 will followed by three lines of data, each line has 5
values. The size of the data set was read in main. Do not input the size in this function. The function
has 4 arguments, the array name, rows, columns, and the file pointer, such as filein (see above).
Write a function, display_2darray, to print the values in the two dimension array so that the numbers are
aligned in columns and each row is printed on a separate line. The function will the use of the setw()
function to align the data. One additional header file will be necessary to use the i/o manipulators #include<iomanip>. The function has 3 arguments, array, rows and columns.
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
cout<<setw(4)<<data[i][j]; // if you use 5 digit numbers setw(7) may be necessary
cout<<endl;
}
Write a function, find_value, to find and save the locations of all occurrences of a specific value passed to this
function. The function will have 6 arguments – the two dimension data array, the rows and columns for
the data array, the locations array, the size for the locations array (passed by reference), and the value to
be found. If the value is found in the data array, store the location/offset where found in the data array
into the locations array. Each time you find the value in the data array the location/offset will be stored
in the next element of the locations array. Search the two dimension data array by rows.
Write a function, display_1darray, to print the values in the single dimension array each on a separate line in the
following format with the following heading.
Offset Row Column
Using the offset the row number can be computed by the following formula: Row = offset/columnSize
Using the offset the column number can be computed by the following formula:
Column = offset%columnSize
Assignment – Put in eCollege dropbox: Program 7
Due: 11/11/13 before class
Use meaningful variable names in your code. Do NOT use global variables in your assignments. This
assignment will be part of your semester grade. If you submit this program late, your assignment grade will be
reduced. Do not copy another student’s code and submit it as yours.

    • 12 years ago
    100% correct answer A++ work Guaranteed the best Tutorial
    NOT RATED

    Purchase the answer to view it

    • 2da.zip