Printing Array Elements in C++

Beginner

Introduction

In C++, arrays are used to store multiple values of the same data type. Sometimes, we may want to print all the values of an array. In this lab, we will learn how to print all the values of an array in C++.

Create a new C++ file

First, let's create a new C++ file named print_array.cpp in the ~/project directory.

touch ~/project/print_array.cpp

Write the code to print an array

In this step, we will write the code to print all the values of an array.

#include <iostream>
using namespace std;

void printArray(int arr[], int size) {
   for(int i=0; i<size; i++) {
      cout<<arr[i]<<" ";
   }
   cout<<"\n";
}

int main() {
   int arr[5] = {1, 2, 3, 4, 5};
   printArray(arr, 5);
   return 0;
}

In this code, we have created a function named printArray that takes two arguments: an array and its size. The function then uses a for loop to iterate through the entire array and print each element.

In the main function, we have initialized an integer array of size 5 with some values and passed it to the printArray function along with its size.

Compile and run the program

Save the print_array.cpp file and open a terminal in the ~/project directory. Use the following command to compile and run the program:

g++ print_array.cpp -o print_array && ./print_array

After running the program, you should see the following output:

1 2 3 4 5

Print the minimum element of an array

Now let's modify the printArray function to print the minimum element of an array.

#include <iostream>
using namespace std;

void printArray(int arr[], int size) {
   int min = arr[0];
   for(int i=0; i<size; i++) {
      if(arr[i] < min) {
         min = arr[i];
      }
   }
   cout<<"Minimum element is: "<<min<<"\n";
}

int main() {
   int arr[5] = {7, 3, 8, 5, 1};
   printArray(arr, 5);
   return 0;
}

In this modified code, we have added a variable named min to store the minimum element of the array. We then use a for loop to iterate through the array and compare each element with the current value of min. If the current element is smaller than min, the value of min is updated. Finally, we print the value of min.

Compile and run the program

Save the print_array.cpp file and use the following command to compile and run the program:

g++ print_array.cpp -o print_array && ./print_array

After running the program, you should see the following output:

Minimum element is: 1

Print the maximum element of an array

Finally, let's modify the printArray function to print the maximum element of an array.

#include <iostream>
using namespace std;

void printArray(int arr[], int size) {
   int max = arr[0];
   for(int i=0; i<size; i++) {
      if(arr[i] > max) {
         max = arr[i];
      }
   }
   cout<<"Maximum element is: "<<max<<"\n";
}

int main() {
   int arr[5] = {7, 3, 8, 5, 1};
   printArray(arr, 5);
   return 0;
}

In this modified code, we have added a variable named max to store the maximum element of the array. We then use a for loop to iterate through the array and compare each element with the current value of max. If the current element is larger than max, the value of max is updated. Finally, we print the value of max.

Compile and run the program

Save the print_array.cpp file and use the following command to compile and run the program:

g++ print_array.cpp -o print_array && ./print_array

After running the program, you should see the following output:

Maximum element is: 8

Summary

In this lab, we learned how to print all the values of an array in C++. We also learned how to find the minimum and maximum elements of an array. By following these steps, you can print all the elements of an array and perform other operations on it.

Other Tutorials you may like