0% found this document useful (0 votes)
26 views3 pages

Array Operations in C++ Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Array Operations in C++ Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

--------------------------------------------------

ARRAY INPUT
--------------------------------------------------
int n;
cout<<"Enter number of array elements: ";
cin>>n;

int array[n];

cout<<"Enter value of array elements: ";

for(int i=0; i<n; i++)


cin>>array[i];

for(int i=0; i<n; i++)


cout<<array[i]<<" ";

--------------------------------------------------------
ADDING TWO ARRAYS
--------------------------------------------------------

#include <iostream>
using namespace std;

int main() {
// Define two arrays
int a[2] = {0, 1};
int b[2] = {2, 3};
int c[2]; // Resultant array

// Add corresponding elements manually


c[0] = a[0] + b[0];
c[1] = a[1] + b[1];

// Print the result


cout << "Resultant Array: " << c[0] << " " << c[1] << endl;

return 0;
}

--------------------------------------------------------

#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter the size of the arrays: ";


cin >> n;

int arr1[n], arr2[n], sum[n];

cout << "Enter elements of the first array: ";


for (int i = 0; i < n; i++) {
cin >> arr1[i];
}
cout << "Enter elements of the second array: ";
for (int i = 0; i < n; i++) {
cin >> arr2[i];
}

for (int i = 0; i < n; i++) {


sum[i] = arr1[i] + arr2[i];
}

cout << "Resultant array after summing: ";


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

return 0;
}

------------------------------------------------------
AVERAGE MARKS USING ARRAY
------------------------------------------------------
#include <iostream>
using namespace std;

int main() {

int students = 5;
float marks[students];
float sum = 0;

cout << "Enter the marks of " << students << " students: ";
for (int i = 0; i < students; i++) {
cin >> marks[i]; // input marks
sum += marks[i]; // add marks to sum
}

float average = sum / students; // calculate average

cout << "The average marks of the students are: " << average << endl;
return 0;
}

-------------------------------------------------------------
AVERAGE MARKS
-------------------------------------------------------------
#include <iostream>
using namespace std;

int main() {

int score = 5;
float marks[score];
float sum = 0;

cout << "Enter the marks of " << score << " students: ";
for (int i = 0; i < score; i++) {
cin >> marks[i]; // input marks
sum += marks[i]; // add marks to sum
}

float average = sum / score; // calculate average

cout << "The average marks of the students are: " << average << endl;

return 0;
}

You might also like