0% found this document useful (0 votes)
36 views

Assignment

The document contains 5 questions related to array traversal and manipulation in C++. Each question provides the code to declare and initialize an array, then perform various operations like printing elements, calculating sums, or transposing a 2D array. For each question, the code takes user input to populate the array, then uses for loops with conditional statements like if-else to traverse and/or manipulate the array elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Assignment

The document contains 5 questions related to array traversal and manipulation in C++. Each question provides the code to declare and initialize an array, then perform various operations like printing elements, calculating sums, or transposing a 2D array. For each question, the code takes user input to populate the array, then uses for loops with conditional statements like if-else to traverse and/or manipulate the array elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Gonzales, Julius Albert P.

BIT-CET 3

Assignment in Data Structure and Algorithm

Question 1: Basic Array Traversal


#include <iostream>

using namespace std;

int main()

int arr[50], size;

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

cin>>size;

cout<<endl;

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

cout<<"Enter Element "<<i+1<<": ";

cin>>arr[i];

cout<<endl;

cout<<"\nElements in an Array:"<<endl;

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

cout<<arr[i]<<"\t";

Question 2: Conditional and Array Traversal


#include <iostream>

using namespace std;

int main() {

int arr[50], size;

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

cin>>size;

cout<<endl;
for(int i = 0; i<size; i++){

cout<<"Enter Element "<<i+1<<": ";

cin>>arr[i];

cout << "\nEven numbers in the array are: "<<endl;

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

if (arr[i] % 2 == 0) {

cout<<arr[i]<<"\t";

cout<<endl;

cout<<endl;

return 0;

Question 3: Reverse and Conditional Array Traversal


#include <iostream>

using namespace std;

int main() {

int arr[50], size;

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

cin>>size;

cout<<endl;

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

cout<<"Enter Element "<<i+1<<": ";

cin>>arr[i];

cout << "\nOdd numbers in reverse order are: "<<endl;

for (int i = size - 1; i >= 0; --i) {

if (arr[i] % 2 != 0) {

cout <<arr[i] << "\t";

}
}

cout <<endl;

return 0;

Question 4: Sum of Array (Even) Elements


#include <iostream>

using namespace std;

int main() {

int arr[50], size;

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

cin>>size;

cout<<endl;

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

cout<<"Enter Element "<<i+1<<": ";

cin>>arr[i];

int sum = 0;

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

if (arr[i] % 2 == 0) {

sum += arr[i];

cout << "\nSum of even numbers in the array: "<<endl;

cout<< sum <<endl;

return 0;

Question 5: 2D Array Traversal


#include <iostream>

using namespace std;

int main() {
int rows, cols;

cout << "Enter the number of rows: ";

cin >> rows;

cout << "Enter the number of columns: ";

cin >> cols;

cout<<endl;

int matrix[rows][cols];

cout << "\nEnter the elements of the matrix:" <<endl;

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

for (int j = 0; j < cols; ++j) {

cout << "Enter element --> row: " << i << " col: " << j << ": ";

cin >> matrix[i][j];

cout << "Transposed matrix:" <<endl;

for (int j = 0; j < cols; ++j) {

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

cout << matrix[i][j] << " ";

cout <<endl;

return 0;

You might also like