Please help! How do I code this?

If anyone can help out please, new to c++ and need help.


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.
Last edited on
If anyone can help out please, new to c++ and need help.


As this is an exercise, the teacher will have covered what is needed to complete this during class. So what part of this exercise are you having difficulty with? What have you done already? Is your program capable of obtaining the two required values using cin? Have you created the 2-d array and used for loops to obtain the data? Show us what you have already and then we'll be able to advise. But as this is homework, won't provide a solution just guidance as it needs to be your own work
I need someone to show me how to do this code or what it should look like. I would like to see the code for this lab I have to do.
Do NOT double post: http://www.cplusplus.com/forum/general/272612/

This is NOT a "we will do your homework for you" site. You do the work. Have problems we will help you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>
#include <limits>

int main()
{
   std::cout << "Do you want someone to do all the work for you? ";
   char answer{};
   std::cin >> answer;

   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   if (std::toupper(answer) == 'Y' ) { std::cout << "Post it in the Jobs section.\n"; }

   else { std::cout << "Show what you have coded so far.\n"; }

   std::cout << "Good luck.\n";
}
I need someone to show me how to do this code or what it should look like. I would like to see the code for this lab I have to do.


As I said in my previous post, what difficulty are you having? What have you already coded? Show your existing code and then we'll provide advice and guidance. If we just write it for you, you'll never learn and you'll get marks for work which won't be yours which is somewhat fraudulent.

If you 'see the code for the lab you have to do' then you'll have the code to provide to your teacher without any work on your part. So no.
the assignment is either asking you to use nonstandard c++ or you are supposed to use pointers or other dynamic containers here.
arrays MUST be coded a hard size in the program, you CANNOT get their size from the user.

if you use better compiler settings, you will see this:
call g++ with at least these:

g++ filename.cpp -Wall -Wextra -std=c++17 -pedantic-errors -O3 -s

you need to get clarification on whether you are supposed to use illegal c++ code (even though it can be made to work in relaxed mode g++) or not.
Last edited on
Hello sayat52,

I would start with this:

In this C++ program, Lab1.cpp, please write code that will do the following things:

1. • Use cin to read two integers x and y
     • Use "row" and "col" for variable names not "x" and "y".

2. • Use x and y to create a two-dimensional array, which has size x (row) and y
(column)

3. • Write a nested for loop to read (cin) char values, assign these values to the
elements of the two-dimensional array

4. • Write another nested for loop to print out the values of the two-dimensional array.


Get at least this much working.

Start small, compile often and fix errors and test as needed.

Andy
Hello sayat52,

To your hints I would make this change:

• It is normal that you will have questions about C++ language itself, you can check the following website
   http://www.cplusplus.com/reference/ Old outdated, but good for a quick reference.
   https://en.cppreference.com/w/ Newer and up to date. Better to use.

These links 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.



Andy
Topic archived. No new replies allowed.