Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Welcome to our blog on printing arrays in C and C++! Arrays are fundamental data structures in programming, allowing us to store and manipulate collections of elements efficiently. Printing arrays is a crucial task in many programs, whether for debugging, displaying results, or user interaction.
The algorithm for printing elements of an array involves iterating through each element of the array and displaying its value. This can be achieved using various looping constructs such as for loops, while loops, or recursion.
Pseudocode
Initialize a loop to iterate through each element of the array.
Access each element of the array.
Print the value of the current array element.
Repeat steps 2 and 3 until all elements of the array have been printed.
C program to Print the Elements of an Array
1. Using For Loop
In this method, a for loop is employed to iterate through each element of the array. The value of each element is printed inside the loop.
C++
C++
#include <stdio.h>
int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]);
printf("Printing array using for loop:\n"); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); }
return 0; }
You can also try this code with Online C++ Compiler
In the recursion method, a recursive function is employed to print the elements of the array. The function recursively calls itself, printing each element until the base case (end of array) is reached.
C++
C++
#include <stdio.h>
void printArray(int arr[], int size, int index) { if (index == size) { return; } printf("%d ", arr[index]); printArray(arr, size, index + 1); }
int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]);
printf("Printing array using recursion:\n"); printArray(arr, size, 0);
return 0; }
You can also try this code with Online C++ Compiler
In C++, array printing can be achieved using similar methods as in C, with the addition of more streamlined features provided by the C++ standard library. This includes range-based for loops and the use of the std::cout stream for output.
1. Using cout Function
In this, we will learn how to print array in C++ using cout. We can directly use the cout function to print the elements of an array This is not the recommended practice to print array in C++.
C++
C++
#include <iostream>
using namespace std;
int main() { /*declaration and initialization of the array*/ int array[5]={31,51,90,67,23}; cout<<array[0]<<” ”; cout<<array[1]<<” ”; cout<<array[2]<<” ”; cout<<array[3]<<” ”; cout<<array[4]<<” ”; return 0; }
You can also try this code with Online C++ Compiler
In this, we will learn how to print array in C++ using recursion. Recursion is an algorithm in which a function calls itself. Recursion can be used to print an array in C++.
C++
C++
#include <iostream>
using namespace std;
void print_array(int arr[] , int length){ /* static variable stores the previous value */ static int j; if(j == length){ j=0; cout<<endl; return; } cout<<arr[j]<<" "; j++; print_array(arr,length); }
int main() { /* declaration and initialization of the array */ int arr[]={31,51,90,67,23,34}; /* get the size of the array. */ int n = sizeof(arr)/sizeof(arr[0]); print_array(arr, n); return 0; }
You can also try this code with Online C++ Compiler
In this, we will learn how to print array in C++ using array iterators. Array iterators functions can be used to print Array in C++. Iterators functions begin and end help to print array in C++.
C++
C++
#include <iostream>
using namespace std;
int main() { /* declaration and initialization of the array */ int array[6]={31,51,90,67,23,34}; copy(begin(array), end(array), experimental:: make_ostream_joiner(cout,” ”)); return 0; }
You can also try this code with Online C++ Compiler
In this, we will learn how to print array in C++ using a while loop. To print array in C++, we can use the while loop to get the elements of an array.
C++
C++
#include <iostream>
using namespace std;
int main() { //declaration and initialization of the array int arr[7]={31,51,90,67,23,34,10}; int i=0; //get the size of the array. int n = sizeof(arr)/sizeof(arr[0]); //loop to print the array while(i < n){ cout<<arr[i]<<" "; i++; } return 0; }
You can also try this code with Online C++ Compiler
To print an array element in C, access the element by its index using square brackets [], then use the printf() function to display its value.
How to print an array of structures in C?
To print an array of structures in C, iterate through each structure element in the array and print each member using printf() or puts().
How to print a sentence in C using array?
To print a sentence in C using an array, declare a character array containing the sentence, then use printf() or puts() to display the array contents as a string.
What is an array in C syntax?
An array in C syntax is declared by specifying the data type of its elements followed by square brackets [] containing the array size or leaving it empty if the size is determined by the number of initializers.
Conclusion
In this blog, we have discussed Program to Print Array in C & C++. Mastering the techniques for printing arrays in C and C++ opens doors to efficient data handling and streamlined program output. Whether utilizing loops, recursion, or leveraging the features of the respective languages, understanding these methods empowers programmers to manipulate and display array data effectively.
To learn more about arrays. Please refer to our articles-