New to c++ and need help. please help me out!
PART II: Read a file, input the data to a two-dimensional matrix ------------------------------------------------------------------------------
Please do the following steps:
1. Create a directory for lab1 by typing
mkdir lab1
2. Change into your lab1 directory:
cd lab1
3. Download 0505matrix file from Canvas to lab1 directory.
4. Notepad++:
Run Notepad++ by typing notepad++ Lab1.cpp
Note: If you use Emacs, you need to type:
emacs Lab1.cpp
In this C++ program, Lab1.cpp, please write code that will do the following things:
• Use cin to read two integers x and y
• Use x and y to create a two-dimensional array, which has size x (row) and y
(column)
• Write a nested for loop to read (cin) char values, assign these values to the
elements of the two-dimensional array
• Write another nested for loop to print out the values of the two-dimensional array.
5. Save
The values should be printout out row-by-row. the file.
6. Now prompt/terminal:
compile your program by typing the following command in command
g++ -Wall Lab1.cpp
"g++" is the name of the C++ compiler. Option -Wall requests all normal warnings.
You may think the executable file generated by g++ is Lab1. In fact, by default, the executable file name is a.exe in Windows (in Mac/Linux, you would see a.out). If you prefer a different executable filename, such as myLab1, you type the following command to compile the source file
g++ -Wall Lab1.cpp -o myLab1
Option -o gives the name that you want the executable file to have. (In Linux, executable files normally have no extension, unlike Windows, where executable files normally have extension .exe.)
7. If your g++ compiler finds any errors, figure out what’s wrong, correct the errors, save the file, and compile the code again. Repeat this procedure until you have a clean compilation.
PART III: Test the code ----------------------------------
Once g++ is able to compile the code, it will create a file called a.exe (in case of Windows) in the same directory.
In the command prompt (Windows), type a.exe < 0505matrix
Note: In case of Mac/Linux:
Once g++ is able to compile the code, it will create a file called a.out in the same directory. In a shell (an xterm or an Emacs buffer called *shell*), type
./a.out < 0505matrix
Here “< “sign means input redirection. Whenever program a.exe tries to read data from keyboard, it will get data from file 0505matrix. The file 0505matrix contains the following data
55
u r a qo f t cn j
k r h pr e a vot z h ga h
The program read the first two values, 5 and 5, to x and y. Then the program uses x and y to create a two-dimensional array. Then it reads the following values and assigns them to each element of the array. After reading is done, the program prints out the values of the array, row by row. For the file of 0505matrix, the result will be
u r a qo f t cn j
k r h pr e a vot z h ga h
Hint:
• The array size must be from the input. Do NOT hard code the array size. For
example, do NOT write a code like: int arr[5][5];
Your program should work with different input, which may have different array sizes. If the array size is hard-coded, the program will not be able to work with the input values of different sizes.
• It is normal that you will have questions about C++ language itself, you can check the following website
http://www.cplusplus.com/reference/
This link points to "Standard C++ Library Reference". There is a lot of information, not all of which will make sense right now, but you should be able to find a description of the data type or the statement that you have question.
• When you think you have found the error, correct it, save the file, recompile it, and execute it to see if the problem is solved.