Skip to content

AvinandanBose/C_Plus_Plus_Intermediate_II

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C_Plus_Plus_Intermediate_II

Arrays

1.An Array is a group of logically related data items of same data types addressed by a common name .

2.All items in array stored in contiguous (physically adjacent) memory locations.

3.As array stored in contiguous(physically adjacent)memory location , we can traverse all the elements(data items) in single run , hence Arrays falls under Linear Data Structure.

Syntax:<dataType> arrayName[<arraySize>]

Note: ArraySize is the total indexes that Array can have data items / elements stored. 
Each elements are accessed by their individual index values.</i>

Types of arrays:

SizeOf() Operator in Arrays

SizeOf() Operator in Arrays basically used to calculate size of an Array (in bytes) and Array's length i.e. number of Rows and Columns in a Matrix(Array). When it is sizeof(arr) → it becomes sizeof(int *) points to the array which have definite and adjacent rows and columns . When it is sizeof(arr_var[0]) → it becomes sizeof(int). For rows i.e. sizeof(arr_var[0]) it calculates sizes of int(4) in columns and gives the result while only columns i.e sizeof(arr_var[0][0]), it calculates size of int(4) only in a single column i.e. constant 4 only and gives the result . For sizeof(arr) i.e. sizeof(int *) multiplies sizeof(int) i.e. 4 with number of rows and columns. While sizeof a pointer is always 8 bytes.

  
Size of Array : sizeof(arr_var)
Row Length : sizeof(arr_var) / sizeof(arr_var[0])
Column Length : sizeof(arr_var[0]) / sizeof(arr_var[0][0]);  
  

Action of Functions in Array

  
One Dimensional
--------------------------
Function Declaration : <return_data_Type> functionName (<dataType1>[],<dataType2>[],......,<dataTypeN-1>[],<dataTypeN>[]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [],<dataType> <arrayVarName2> [],......,<dataType> <arrayVarNameN-1>[],<dataType> <arrayVarNameN>[]);
Function Call:   functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
  
 If any Array is returned from a function :
  
Return :  return <arrayVarName>[Size of Array];
  
  

Note : Its not necessary that if swapping or any other functional action done inside function, we have to return array with its size i.e. array[size], We can also return its size only.

  
int swapping(int array[], int size){
 int pos1, pos2;
 cout << "Enter the position of the first element: ";
 cin >> pos1;
 cout << "Enter the position of the second element: ";
  cin >> pos2;
  int temp = array[pos1];
  array[pos1] = array[pos2];
  array[pos2] = temp;
  
  return a[size];                        
}

Not necessary we return a[size] :
                           
int swapping(int array[], int size){
 int pos1, pos2;
 cout << "Enter the position of the first element: ";
 cin >> pos1;
 cout << "Enter the position of the second element: ";
 cin >> pos2;
 int temp = array[pos1];
 array[pos1] = array[pos2];
 array[pos2] = temp;
  
  return size;                        
} 
  
Already Swapping Conducted under the body of the function;  

That is we can return anything but if swapping or assignment or any other functional actions is done with the array its already gets applied to the array under the body of function and we can get the swapped array if we dispaly it.





  
Two Dimensional
--------------------------
We have to declare the column size first:

(with define macro)

#define size value[value→Value are integers such as 1,2,3 , ....etc.]
  
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value],<dataType2>[][size/value],......,<dataTypeN-1>[][size/value],<dataTypeN>[][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value],<dataType> <arrayVarName2> [][size/value],......,<dataType> <arrayVarNameN-1>[][size/value],<dataType> <arrayVarNameN>[][size/value]);
Function Call:   functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
  
 If any Array is returned from a function :
  
Return :  return <arrayVarName>[Size of Row][size/value];

(with const (constant) modifier)

const int size = value; [value→Value are integers such as 1,2,3 , ....etc.]

Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value],<dataType2>[][size/value],......,<dataTypeN-1>[][size/value],<dataTypeN>[][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value],<dataType> <arrayVarName2> [][size/value],......,<dataType> <arrayVarNameN-1>[][size/value],<dataType> <arrayVarNameN>[][size/value]);
  
Function Call:   functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
  
 If any Array is returned from a function :
  
Return :  return <arrayVarName>[Size of Row][size/value];
  
  
  
Three Dimensional
--------------------------
We have to declare both the column size and row size first:

(with define macro)

#define size value[value→Value are integers such as 1,2,3 , ....etc.]
  
Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value][size/value],<dataType2>[][size/value][size/value],......,<dataTypeN-1>[][size/value][size/value],<dataTypeN>[][size/value][size/value]);
Function Definition :<return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value][size/value],<dataType> <arrayVarName2> [][size/value][size/value],......,<dataType> <arrayVarNameN-1>[][size/value][size/value],<dataType> <arrayVarNameN>[][size/value][size/value]);
Function Call:   functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
  
 If any Array is returned from a function :
  
Return :  return <arrayVarName>[No. of Page][size/value][size/value];

(with const (constant) modifier)

const int size = value; [value→Value are integers such as 1,2,3 , ....etc.]

Function Declaration : <return_data_Type> functionName (<dataType1>[][size/value][size/value],<dataType2>[][size/value][size/value],......,<dataTypeN-1>[][size/value][size/value],<dataTypeN>[][size/value][size/value]);
Function Definition :  <return_data_Type> functionName (<dataType> <arrayVarName1> [][size/value][size/value],<dataType> <arrayVarName2> [][size/value][size/value],......,<dataType> <arrayVarNameN-1>[][size/value][size/value],<dataType> <arrayVarNameN>[][size/value][size/value]);
  
Function Call:   functionName(<arrayVarName1>,<arrayVarName2>,.....,<arrayVarNameN-1>,<arrayVarNameN>);
  
 If any Array is returned from a function :
  
Return :  return <arrayVarName>[No. of Page][size/value][size/value];
  
  

Note : Though we make tables and represent Array as Row and Columns they are actuallly adjacent memory locations arranged one after the other as shown below:

    1D- Array:

    2D- Array:

Hence After passing 2-D and 3-D arrays in Function Parameter like : int arr[][] (2D)and int arr [] [] [](3D) , it can take 1st block of memory without its size mentioned but next adjacent blocks it asks user the size and the program cannot run without it . As it fail to refer the next adjacent chunk of memory without knowing its size. It is a disadvantage as we cannot use the next memory as user defined during runtime and we have to make everytime the size constant during compilation. The above problem can be solved using pointers and pointer arithmetic.

NULL POINTER in Array

NULL is defined as Zero . Such as in character '\0' is Null character i.e. it is present at the end of array of character whose value is 0.

int size=1 , *ptr;
size = size - 1; // i.e. 0 now
ptr = &size;
if(*ptr == NULL){
exit(0);
}

Similar example is presented below :

  • 1. NULL POINTER EXAMPLE
    • Note: The above will generate a warning i.e. NULL used in arithmetic . The warning can be removed by providing zero.

      int size=1 , *ptr;
      size = size - 1; // i.e. 0 now
      ptr = &size;
      if(*ptr == 0){
      exit(0);
      }
      

      Or

      int size=1 , *ptr;
      size = size - 1; // i.e. 0 now
      ptr = &size;
      if(ptr == NULL){
      exit(0);
      }
      

      Hence: The warning occurs as NULL checks the address of pointer variable ptr is zero and whenever we try to return a value through reference of address it gives a warning as it want a address not a value . We can continue with the warning.

Recursive Array /Recursion Of Array

Note : Recursive call of a function starts from first initial starting point of function and ends till it reach the base case while in Loop , Loop starts from the starting condition of Loop and run till the condition ends(returns false by relational operators) , hence inside Recursive function , we focus only on condition rather assigning any other value inside the recursive function required for the desired output as when recursive function gets called , it again starts from first initial starting point of function, while in loop only loop block is focused which can run inside any function and we can assign any variable outside the loop block as per the condition to get the desired output.

Suppose:

int main(){
int a = 5;
func( a, 0);

}
int func(int a ,  int i) //Function's Initial starting point.
{
    int count = 0;

    //Base Condition

      if(i==a){
          return count;
        }
        
        //Relational Condition and Iteration
      if( i < a){
            count++;
            func(a, i+1);
       }
       
 }
 
 Note : The Result will be 0 only as function call starts from function's starting point.
  • Example 1
  • While in Loop:

    Suppose:
    
    int main(){
    int a = 5;
    func( a, 0);
    
    }
    int func(int a ,  int i) 
    {
        int count = 0;
    
        for(i =0 ;i < a; i++) //Starting of Loop
        {
            count = count +1;  //body of the loop and the loop block
        }
         
         return count;
           
     }
     
     Note : The Result will be 5 as the Loop only run its block .
    
  • Example 2
  • This is the basic difference between loop and recursive function.

    Pointers in Arrays

    Accessing Array of Constants Through Pointers

    About

    About Its an intermediate guide on C++ (II)

    Resources

    License

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Languages