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
  • 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, 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...

  • 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, there I would like to get a answer for this one. Could you please help...

    Hi, there I would like to get a answer for this one. Could you please help me? It's java thanks a lot Write a method that takes an array of booleans, theArray, and returns the "Yes" if the last element is true and "Nope" otherwise The method signature should be: public String check (boolean[] theArray) For example: Test Result System.out.println(check(new boolean[]{true, true, true})); Yes System.out.println(check(new boolean[]{true, true, true, false})); Nope

  • 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.

  • can you please help me out on creating this product and all it's costs? i'm very...

    can you please help me out on creating this product and all it's costs? i'm very confused, thank you in advance!!!! That is the full question, I have to create a product and include/create all that info for it Please identify a product you will manufacture. Please complete the following steps for your product: 1) List at least twelve costs related to your product - you much use each category at least once. 2) Identify the per unit cost for...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • hi i dont know and understand what this assignment wants me to do can some please...

    hi i dont know and understand what this assignment wants me to do can some please explain it to me thank you Modify the print_face function to have a third parameter mouth and use it in the final print statement. def print_face(eye, face_char): print(' ', eye, ' ', eye) # Print eyes print(' ', face_char) # Print nose print(' ', face_char*5) # Print mouth return print_face('o', 'x')

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
ADVERTISEMENT