Question

hi, I'm a new student of c++, I found it's difficult to understand the class, can...

hi, I'm a new student of c++, I found it's difficult to understand the class, can you guys please give me an example of how to create a two dimensional array in a class. like create an array[3][4] in a class, let the user input the value of array. Thank you.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Definition - Array:

  • An array is a data structure that is used to store the fixed size of sequential collection of data.
  • An array stores the elements of same datatype.

Advantage:

  • If a user or customer needs to store 100 elements of type integer, it is very difficult to create 100 variables which results in time and space complexities.
  • To store these collection of elements of same data type we use arrays.

Declaring an array in c++:

  int balance[5];

int   - integer data type

balance - array name

5   - size of the array

2 dimensional Array:

Syntax: data_type array_name [size1][size2];

example:

int balance[4][3];

The above example creates an array with 4*3 = 12 elements.

Here 4 denotes the number of rows

3 denotes the number of columns.

Description:

  • A 2 dimensional array is similar to matrix of m rows and n columns.
  • It can be represented as tabular format.
  • An array with 2 rows and 3 columns are represented as
Column 0 column 1 column 2
Row 0 Array[0][0] Array[0][1] Array[0][2]
Row 1 Array[1][0] Array[1][1] Array[1][2]

C++ program for 2 dimensional array:

#include <iostream>

using namespace std;
class TwoDimensional
{
public: //access specifier
void createArray()
{
int row, col; //varibale declaration
cout << "Please enter the rows size: " << endl; //message displayed to user.
cin >> row; //to read data(number of rows) from user
cout << "Please enter the column size: " << endl;
cin >> col; //to read data(number of columns) from user

cout << "Please enter the numbers you want to put into a 2D array" << endl;
cout << "Press enter after each number you input: " << endl;

int arr[row][col]; //2 dimensional array declaration
//for loop to read the 2 dimensional array from user
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; j++) {
cin >> arr[i][j];
}
}
  
//for loop To print the array
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
};

int main()
{
TwoDimensional td; //To create an object(td) for the class TwoDimensional
td.createArray(); //function call to createArray
return 0;
}

Output:

Please enter the rows size: Please enter the column size: Please enter the numbers you want to put into a 2D array Press ente

Add a comment
Know the answer?
Add Answer to:
hi, I'm a new student of c++, I found it's difficult to understand the class, can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would I reply to this? please help I'm stuck It's important for companies to understand...

    How would I reply to this? please help I'm stuck It's important for companies to understand which key resources contribute to their comparative advantage. For example , for a company like Google intelligence and human resources drive the force of their business forward. With new and innovative ideas this can be used along with the other resources to create revenue. Do you believe one type of key resource to be more important than the other?

  • Hi so we have 4 labs in our introduction to c++ class for this week and...

    Hi so we have 4 labs in our introduction to c++ class for this week and this is the only exercise that I couldn't understand thank you guys //Lab exercise 4: //Ask the user to input 10 probabilities (numbers between 0 and 1). Then output the index(es) of the number(s) with highest probability. //Example input: 0.8 0.8 0.8 1 1 1 1 1 0.8 0.8 //Example output: 3 4 5 6 7

  • Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student...

    Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student gradebook using a two-dimensional array. It should allow the user to enter the number of students and the number of assignments they wish to enter grades for. This input should be used in defining the two-dimensional array for your program. For example, if I say I want to enter grades for 4 students and 5 assignments, your program should define a 4 X 5...

  • Hi. I'm a student learning wireless communication system. I would like to know exactly why MMSE...

    Hi. I'm a student learning wireless communication system. I would like to know exactly why MMSE always shows better performance when comparing ZF and MMSE in a MIMO system with a Rayleigh channel. Thank you.

  • Hi, I need a program written in Python that can translate a phone number into letters....

    Hi, I need a program written in Python that can translate a phone number into letters. The program needs to prompt the user to input a phone number of their choice to translate. I'm in a basic level python class so I would appreciate it if the program wasn't too high skilled or included "char" if possible. Thank you in advance.

  • hi, please help in c++. I dont understand how to do this, and a lot of...

    hi, please help in c++. I dont understand how to do this, and a lot of the ways on the internet is confusing me, i am a beginner. with all steps and explantions, will rate! Write a program that determines the frequency of each char in an input file. Your program should . read in an unknown number of chars from an input file named input.txt, • using a function written by you, determine the frequency of occurrence of each...

  • Hi quick question can you solve this for me cause I'm a little bit confuse about...

    Hi quick question can you solve this for me cause I'm a little bit confuse about this one thanks! this is c++ and yes I'll give you a thumbs up thank you ^^ Write a program that reads in temperatures. Use a while loop (or a do..while loop, if you prefer, but the while loop might be easier). When the user enters -999, the loop exits. Then, print out the average temperature, the highest and the lowest temperature entered.

  • You will be designing and creating a Java GUI-based course application. Create a “Student” class. You...

    You will be designing and creating a Java GUI-based course application. Create a “Student” class. You need to have at least 3 instance variables (student characteristics like name,…), at least 2 constructors (1 should be a no-arg constructor), set and get methods. Create a “Course” class that consists of 2 instance variables: the course name and an array of Students (using your Student class). Design and create a JavaFX-based GUI interface that allows you to : INPUT information for Course...

  • Can someone please explain what Economic Input-Output Life Cycle Analysis is in the dumbest terms possible,...

    Can someone please explain what Economic Input-Output Life Cycle Analysis is in the dumbest terms possible, and give me a good example with reference to materials such as steel, I am a Civil engineer student and having a difficult time understanding the concept!! Thank you!!

  • Hi, I need help understanding a concept in my Assembly Language (MIPS) class. Can someone please...

    Hi, I need help understanding a concept in my Assembly Language (MIPS) class. Can someone please explain the following to me: 3) Implementing Control Structures – Know how to write if-then or if-then-else equivalent statements in assembly as well as loops. This is a concept that is going to be asked on my exam. There's no real solution to this question, but I will rate whomever helps me understand this material. An example would be GREATLY appreciated.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT