Collecting array entries and displaying it like its formal representation – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2510 siteicon   siteicon   siteicon  

Collecting array entries and displaying it like its formal representation

Taking array entries from the user and displaying it like its formal representation as {1,2,..5}

Learning Objectives

  • Collecting all entries of an array from the user.
  • Defining size of array using the pre-processor define macros.
  • Trick of not printing last comma when printing comma separated array members.
  • Neat printing of array like a real array representation.

Source Code

TC++ #2510

Source Code

Run Output

Enter member 1 : 11
Enter member 2 : 32
Enter member 3 : 9
Enter member 4 : 10
Enter member 5 : 99

The array you entered is :
{11,32,9,10,99}

Code Understanding

#define asize 5   
This is an easy technique to define the array size outside the main, so that the word asize can be replaced by 5 everywhere the compiler finds it. This most common use of preprocessor macros.

int a[asize]; //This will be read as compiler as int a[5];  defining an array which can take 5 integer members.
int cnt=asize; //This will be taken as int cnt=5; It will make it easy for programmer to use the size variable everywhere else in the program.
for(int i=0;i<cnt;++i) //This loop run from 0 to <4 means 5 iterations will happen in this case.
{
cout<<“Enter member “<<i+1<<” : “; cin>>a[i];
Here we collect each member of array. Since array index starts from 0 and array position as known to real world begins from 1 so we write i+1, so that user can be shown which position he is entering the member for.
}
cout<<endl<<“The array you entered is :”<<endl; //This is prompt before array display
cout<<‘{‘; //To show the real array like display we begin it by printing { . We print it before the array printing loop as it has to be printed just once.

for(int i=0;i<cnt;++i) //This is array printing loop
{
cout<<a[i]<<((i<=cnt-2)?”,”:””);
Here we print the array member followed by a , (comma). Since the last member will not have a leading comma we will use the ternary expression to achieve this goal.  In ternary expression we print comma till we are printing last but one member. This is why we subtract 2 from cnt to determine the last but one member. Up to pre-last member the expression (i<=cnt-2) is true so we print “,” and later when it is false we just do not print anything so “” has been used.
}
cout<<‘}’;  //This is final curly braces of the array to be printed.
}

 

Notes

  • Array entries are often collected from the user so that the data can be first stored in memory locations and then final processing can be done on it later.

Common Errors

  • If students are committing repeated mistakes in printing it in the right format. They must first try to print array members with leading spaces only. Later they can refer to a similar example at #2042 or they can learn more about this concept at #1728 .


Suggested Filename(s): arr-inp-disp.cpp



Share

sunmitra| Created: 12-Jan-2018 | Updated: 12-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