Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions C++/Array/ArrayDemo1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


#include<iostream>
using namespace std;

int main()
{
// interger array of five elements
int marks[5];

// assigning values
cout << "Please enter the marks of student 1 : " ;
cin >> marks[0] ;

cout << "Please enter the marks of student 2 : " ;
cin >> marks[1] ;

cout << "Please enter the marks of student 3 : " ;
cin >> marks[2] ;

cout << "Please enter the marks of student 4 : " ;
cin >> marks[3] ;

cout << "Please enter the marks of student 5 : " ;
cin >> marks[4] ;


// accessing array values
cout << "marks[0] :" << marks[0] << endl;
cout << "marks[1] :" << marks[1] << endl;
cout << "marks[2] :" << marks[2] << endl;
cout << "marks[3] :" << marks[3] << endl;
cout << "marks[4] :" << marks[4] << endl;



cout << "size of marks array : " << sizeof(marks) << endl;

return 0;
}
30 changes: 30 additions & 0 deletions C++/Array/ArrayDemo2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


#include<iostream>
using namespace std;

int main()
{

int size;
cout << "Please enter the strenght of students in your class : ";
cin >> size;

// interger array of user defined size
int marks[size];

// assigning values
for(int i=0; i<size; i++)
{
cout << "Please enter the marks of Student " << (i+1) << ": ";
cin >> marks[i];
}

// accessing array values
for(int i=0; i<size; i++)
{
cout << "marks[" << i << "]: " << marks[i] << endl;
}

return 0;
}
41 changes: 41 additions & 0 deletions C++/Array/ArrayDemo_boundry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


#include<iostream>
using namespace std;

int main()
{
// interger array of five elements
int marks[5];

// assigning values
marks[0] = 85;
marks[1] = 85;
marks[2] = 85;
marks[3] = 85;
marks[4] = 85;
marks[5] = 85; // the indexes out of initialization-size
marks[6] = 85;

marks[10] = 85;


// accessing array values
cout << "marks[0] :" << marks[0] << endl;
cout << "marks[1] :" << marks[1] << endl;
cout << "marks[2] :" << marks[2] << endl;
cout << "marks[3] :" << marks[3] << endl;
cout << "marks[4] :" << marks[4] << endl;
cout << "marks[5] :" << marks[5] << endl;
cout << "marks[6] :" << marks[6] << endl;
cout << "marks[7] :" << marks[7] << endl;
cout << "marks[8] :" << marks[8] << endl;
cout << "marks[9] :" << marks[9] << endl;
cout << "marks[10] :" << marks[10] << endl;



cout << "size of marks array : " << sizeof(marks) << endl;

return 0;
}
26 changes: 26 additions & 0 deletions C++/Array/ArrayDemo_boundryCheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


#include<iostream>
using namespace std;

int main()
{
// decalre + initialize
int marks[5] = {50, 60, 70, 80, 90 };

int index;
cout << "Please enter the roll number for which you want to see the marks " ;
cin >> index ;

if (index >=1 && index < 6) // boundary check
{
cout << marks[index-1];
}
else
{
cout << "Error: You entered wrong roll number... Please try again" << endl;
}


return 0;
}
21 changes: 21 additions & 0 deletions C++/Array/ArrayDemo_initialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


#include<iostream>
using namespace std;

int main()
{
// decalre + initialize
int marks[5] = {50, 60, 70, 80, 90 };


// accessing array values
for(int i=0; i<5; i++)
{
cout << "marks[" << i << "]: " << marks[i] << endl;
}



return 0;
}
25 changes: 25 additions & 0 deletions C++/Array/Array_Associativity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


#include<iostream>
using namespace std;

int main()
{
int marks[5];
string name[5];

for (int i=0; i<5; i++)
{
cout << "Please enter the name of student : " ;
cin >> name[i];

cout << "Please enter the marks of " << name[i] << " : ";
cin >> marks[i];

}

for(int i=0; i<5; i++)
cout << name[i] << "---" << marks[i] << endl;

return 0;
}
51 changes: 51 additions & 0 deletions C++/Array/Array_Function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@


#include<iostream>
using namespace std;

void printInfo(int[] , int); //prototype
void modify(int[] , int); //prototype

int main()
{
int size = 5;
int marks[size];
string name[size];

for (int i=0; i<size; i++)
{
cout << "Please enter the name of student : " ;
cin >> name[i];

cout << "Please enter the marks of " << name[i] << " : ";
cin >> marks[i];
}

printInfo(marks, size); // by ref
modify(marks, size);

// displaying after modification
for(int i=0; i<size; i++)
cout << marks[i];


return 0;
}


void printInfo(int val[] , int sz)
{
cout << "The control enters into printInfo() " << endl;

for(int i=0; i<sz; i++)
cout << val[i] << endl;
}


void modify(int val[] , int sz)
{
cout << "The control enters into modify() " << endl;

for(int i=0; i<sz; i++)
val[i] = 100;
}
44 changes: 44 additions & 0 deletions C++/Array/Array_Similarity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


#include<iostream>
using namespace std;

int main()
{
// interger array
int number[5] = {10, 20, 30, 40, 50};
int data[5] = {10, 20, 30, 40, 60};

cout << "Address of number : " << number << endl;
cout << "Address of data : " << data << endl;

// Match array references
if(number == data)
cout << "The arrays are equal. " << endl;
else
cout << "The arrays are not equal." << endl;

// Matching array elements one by one.
bool flag = true;
int index;
for (int i=0; i<5; i++)
{
if(data[i] != number[i])
{
flag = false;
index = i;
}
}

if(flag)
cout << "The array elements are equal. " << endl;
else
{
cout << "The array elements are not equal." << endl;
cout << "Mismatched index is : " << index << endl;
}



return 0;
}
24 changes: 24 additions & 0 deletions C++/Array/Array_processing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


#include<iostream>
using namespace std;

int main()
{
// interger array of five elements
int number[5] = { 15, 25, 35, 45, 55 };


int value = number[2]++;
cout << value << endl; // ??? 35?

for(int var:number)
cout << var << endl;


int index = 2;
int value1 = number[index++];
cout << value1 << endl;

return 0;
}
28 changes: 28 additions & 0 deletions C++/Array/Array_processing1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


#include<iostream>
using namespace std;

int main()
{
// interger array of five elements
int number[5] = { 15, 25, 35, 45, 55 };

int sum = 0;
for(int var:number)
{
sum = sum + var;
}
cout << "Sum :" << sum << endl;
cout << "Avg :" << sum/5.0 << endl;

int add=0;
for(int i=0; i<=4; i++)
{
add = add + number[i];
}
cout << "Add :" << add << endl;
cout << "Avg :" << add/5.0 << endl;

return 0;
}
30 changes: 30 additions & 0 deletions C++/Array/Array_processing2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


#include<iostream>
using namespace std;

int main()
{
// interger array
int number[10];
int counter = 0; // maintain the counting of properly initialized elements

//initialize
for(int i=0; i<5; i++)
{
cin >> number[i];
counter++;
}


cout << "The value of counter : " << counter << endl;

int sum=0;
for(int i=0; i<counter; i++)
{
sum = sum + number[i];
}
cout << "sum: " << sum << endl;

return 0;
}
Loading