Copying an Integer Array – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#3091 siteicon   siteicon   siteicon  

Copying an Integer Array

Demonstrating how an array can be copied to another array of same size by copying each member of the array.

Learning Objectives

  • Copying an array to another array of same size by copying each member of the array.

Source Code

TC++ #3091

 

Source Code

Run Output

Members of source array :
25 10 32 44 99
Members of destination array :
25 10 32 44 99

Code Understanding

int a1[5]={25,10,32,44,99}; //array a1 declared and initialised with 5 members
int a2[5];                                 //array a2 of same size as a1 declared.

cout<<“Members of source array : “<<endl;
for(int i=0;i<5;i++) cout<<a1[i]<<” “;
this loop shows members of array a1 by simply displaying member at each index.

for(int i=0;i<5;i++) a2[i]=a1[i];
This loop does the copy work by copying each member of array a1 to array a2 by using command  a2[i]=a1[i]

cout<<endl<<“Members of destination array : “<<endl;
for(int i=0;i<5;i++) cout<<a2[i]<<” “;
This loop displays the copied array

Notes

  • This program will work well with any other type of data type as well.
  • After c++11 you have another direct method of creating a copied array as follows.
    array<int,4> A = {10,20,30,40};
    array<int,4> B = A;

Common Errors

  • Student some time write copy statement as a1[i]=a2[i]. This is incorrect. Remember always the right side is assigned to left hand side. So it should definitely be a2[i]=a1[i];


Share

sunmitra| Created: 27-Jan-2018 | Updated: 27-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back