This program increments every element of the array by one & print incremented array.
This is a C program which increments every element of the array by one & print array. We need to add 1 to all the elements of the given array.
1. Create an array of some size and define its elements.
2. Create a function in which the array created will be passed as parameter.
3. Inside this function, using for loop, access each element of the array, add 1 to the element and store this new value in the same place.
4. Print the array.
Here is source code of the C Program to increments every element of the array by one & print array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Increment every Element of the Array by one & Print Incremented Array
*/
#include <stdio.h>
void incrementArray(int[]);
void main()
{
int i;
int array[4] = {10, 20, 30, 40};
incrementArray(array);
for (i = 0; i < 4; i++)
printf("%d\t", array[i]); // Prints 2, 3, 4, 5
}
void incrementArray(int arr[])
{
int i;
for (i = 0; i < 4; i++)
arr[i]++; // this alters values in array in main()
}
1. Declare an array of some fixed size(i.e 4) and define all its element at the time of declaration.
2. Create a function an pass this array to this function as a parameter.
3. Inside this for loop, run a for loop from 0 to size-1, accessing each element of the array, add 1 to it and store the result in the same array index.
4. Print the array.
11 21 31 41
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Check C Books
- Practice BCA MCQs
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos