2D Arrays & Loops

Objective: of this lab Manual is to get hands-on experience of 2D Arrays & Loops in C++. Get familiar with the syntax, Errors, and implementations.

Background: 

Declaring an Array: Use the following syntax below.
DataType ArrayName [ConstIntExpression];
The example below shows the declaration of an integer array of size 10 with element 0 - 9. 
const int MAXSIZE = 10;
int array[MAXSIZE];
Arrays can be initialized during declaration by equating the array to a listing of the array's members in brackets.  For example  
int array[MAXSIZE] = {2 , 4, 6, 8, 10, 12, 14, 16, 18, 20};
Passing Arrays as Function Parameters In C++, arrays are always passed by reference by default.  Whenever an array is passed as a parameter, its base address is sent to the called function. Generally, functions that work with arrays require 2 items of information as actual parameters: the beginning address of the array (in memory), and the number of elements to process in the array. For example (Function PrintArray): 
void PrintArray(int Array[], int ArraySize) {   
 for (int i = 0; i <= ArraySize - 1; i++)      
                cout << "array[" << i << "] = "<< Array[i] << endl;
}
Declare and manipulate Two-dimensional arrays  Two-dimensional arrays store a tabular arrangement of values accessed by two indexes, for example matrix[i][j], where i is the row index and j is the column index.  To declare and initialize a two-dimensional arrays, Use the following syntax below.
DataType ArrayName [row][column];
The example below shows the declaring two-dimensional array b
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

Assessment tools:

The assessment is according to the student participation in the lab if the student solves the exercise, he will get the participation mark for this lab.

Lab Assignments:

Q1- Write a program in C++ to declare 2D integer array with a size of 3X5 initialize it with zero and print values on the console?

Q2- Write a program in C++ to declare A 2D array of any type and size take entries from user and display values on the console?


Q3- Write a program in C++ to declare A 2D array of any type with size 3X3 your first column of the 2D array should hold values from 0-9 and the rest of the values should be more than 9?

Q4- Write a program in C++ to declare a 2D array of character type with size 3X3 your array should hold only character values if user enter any other value it should print invalid entry?

                       
Q5- Write a program in C++ to declare A 2D array of any type with any size print only even indexes values on the console?


Q6- write a program in C++ to calculate the size of 2D array your program should display only the size of the declared array.

Q7- Write a program in c++ to typecast your declared variables' values for this task you need to use the build-in function for type casting use int() i.e. this mentioned function will change any value to an integer and you can use the syntax for other data types as well datatypes( variable ).


Q8- write a program in C++ to use build-in functions such as Sum and Average defined in Math.h library.

Q9- write a program in C++ to declare string variable and take input from the user and display its output to the console.


 Q10- write a program in C++ to calculate the size of the string data type you can use sizeof(), operator for this task.


Please All Students submit your assignment again at the given link:
















7 Comments

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. CMS 405646
      Q1

      #include
      using namespace std;
      int main()
      {
      int abc[3][5];
      cout<<"enter your numbers :";

      int i,j;

      for(i=0; i<3; i++)
      {
      for( j=0; j<5; j++)
      {
      cin>>abc[i][j];
      }
      }

      system("pause");

      return 0;

      }

      Delete
  2. CMS 405646
    Q 2
    #include
    using namespace std;
    int main()
    {
    int arr[2][2];
    int i,j;

    cout<<"enter your numbers :"<>arr[i][j];
    }
    }


    system("pause");
    return 0;


    }

    ReplyDelete
  3. #include
    using namespace std;
    int main()
    {
    int array[5][5];
    int array1[2][2]={1,2,3,4};
    int array2[3][3]={{1,1,1},{2,2,2},{3,3,3}};
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    cout<<array2[i][j]<<" ";
    }
    cout<<endl;
    }
    system("pause");
    return 0;
    }
    name Ahtsham
    CMS 406016
    A1 BSCS

    ReplyDelete
  4. Q#1
    #include
    using namespace std;
    int main ()
    {
    int arr[3][5]={0};
    int i,j;
    for(i=0; i<=2; i++)
    {
    for(j=0; j<=4; j++)
    {
    cout<<arr[i][j];
    }
    system ("pause");
    }
    return 0;
    }

    ReplyDelete
  5. Sir Kindly Elaborate the Quetion No. 4. Any key entered by a user is treated as a character on run time. "if user enter any other value it should print invalid entry?" What does this thing mean?

    ReplyDelete
  6. Please guide us about Q8. math.h library does not have sum and average functions.

    ReplyDelete

Post a Comment

Previous Post Next Post