Arrays & Loops

Objective: of this lab Manual is to get hands-on experience of 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 merge tow arrays of any size you must declare integer arrays and then concatenate them.
Example: A={1,2,3,4,5} B={6,7,8,9,0}    after concatenation C={1,2,3,4,5,6,7,8,9,0}
#include<iostream>
using namespace std;
int main()
{                      
  int ae[5];
  int ar[5];
  int as[10];
  int i,j;
  for(i=1;i<=5;i++)
  {
              cin>>ae[i];
  }
  for(j=1;j<=5;j++)
  {
              cin>>ar[j];
  }
  for(i=1;i<=5;i++)
  {
              as[i]=ae[i];
             
  }
  for(i=6,j=1;i<=10,j<=5;i++,j++)
  {
              as[i]=ar[j]; 
  }
  for(i=1;i<=10;i++)
  {
              cout<<as[i]<<endl;
  }
  system("pause");
  return(0);
}

Q2- Write a program in C++ to search element in the Integer array of any size if entered element present in the array your code should print “Element present in the array” on the console.
Output:
A={1,2,3,4,5}
Element for searching=4
“Element present in the array”
#include<iostream>
using namespace std;
int main()
{
 
  int ae[4];
  int j,d;
  cout<<"Type Values:"<<endl;
  for(j=1;j<=4;j++)
  {
              cin>>ae[j];
  }
  cout<<"Type the value you want to search:"<<endl;
  cin>>d;
  for(j=1;j<=4;j++)
  {
              if(ae[j]==d)
              {
                          cout<<"Element Found"<<endl;
                          j=6;
              }
              else
              {
                          cout<<"Not Present"<<endl;
                          j=6;
              }
  }
              system("pause");
              return(0);
}


Q3- Write a program in C++ which convert the elements of whole character array into Capital letters for instant if array holds a,b,c it will convert them in A,B,C etc.
#include<iostream>
using namespace std;
int main()
{
            char ar[5];
            int i;
            cout<<"Type Alphabets in small case:"<<endl;
            for(i=1;i<=5;i++)
            {
                        cin>>ar[i];
                        if(ar[i]>='a',ar[i]<='z')
                        {
                                    ar[i]=ar[i]-32;
                                    cout<<ar[i]<<endl;
                        }
            }
            system("pause");
            return(0);
}









Q4- Write a program in C++ to delete element in the Integer array of any size if entered element present in the array your code should print “Element Deleted” on the console.
Output:
A= {1, 2, 3,4, 5}
Element for searching=4
“Element Deleted”
A= {1, 2, 3, 5}


Q5- Write a program in C++ to sum same indexes of two arrays and return the sum on the console for example A={1,2,3,4} B={1,1,1,1} Sum={2,3,4,5} etc.

Note: you must take the size of arrays they must be equal for this task if they are not equal then you must print it cannot be possible to add them

Note: you must take the size of arrays they must be equal for this task if they are not equal then you must print it cannot be possible to add them.

Q6- write a program in C++ to initialize a 2D array. Take input & display on the console. (Bonus points)


Submission Link:
                                             Click for Submission 


2 Comments

  1. #include
    using namespace std;
    int main()
    {

    int ae[4];
    int j,d;
    cout<<"Type Values:"<>ae[j];
    }
    cout<<"Type the value you want to search:"<>d;
    for(j=1;j<=4;j++)
    {
    if(ae[j]==d)
    {
    cout<<"Element Found"<<endl;
    j=6;
    }
    else
    {
    cout<<"Not Present"<<endl;
    j=6;
    }
    }
    system("pause");
    return(0);
    }

    ReplyDelete
  2. #include
    using namespace std;
    int main()
    {

    int ar[4];
    int j,e;
    cout<<"Type Values:"<<ar[j];
    {
    cout<<"Type the value you want to search:"<<e;
    for(j=1;j<=4;j++)
    {
    if(ar[j]==e)
    {
    cout<<"Element Found"<<endl;
    j=6;
    }
    else
    {
    cout<<"NOT FOUND "<<endl;
    j=6;
    }
    system("pause");
    return(0);
    }
    }
    }

    ReplyDelete

Post a Comment

Previous Post Next Post