Note: This is a very simple program but still a very important one, because we’ll be using some form of logic to print elements of an array. So better we know ins and outs of printing array elements in whichever order the program demands. So please pay attention to the logic.
Video Tutorial: C Program To Print Elements of Array In Reverse Order
Source Code: C Program To Print Elements of Array In Reverse Order
#include<stdio.h>
int main()
{
int a[5], i;
printf("Enter 5 integer numbers\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 4; i >= 0; i--)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 5 4 3 2 1
Since the array size is 5, the last index of the array will be (5-1) which is 4. So we initialize i to 4, and keep decrementing the value of i by 1 for each iteration of the for loop. Control exits for loop once i value is equal to 0. In arrays the index starts from 0. Inside for loop, for each iteration, we print the value of i.
#include<stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = N-1; i >= 0; i--)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 5 4 3 2 1
Here we initialize value of i to the last index of the array, which is N-1. We iterate through the for loop until i value is 0(which is the first index of the array), for each iteration of the for loop we decrement the value of i by 1. Inside for loop we print the value of a[i].
Disadvantage of Not Using Macro or Constant To Assign Array Size
If requirement of the program/software changes and you need to increase or decrease the array size, then you’ll have to be very careful and scan through the entire source code and make changes at multiple locations. Even if you skip changing the array size information at one place, you’ll start getting wrong results.
And if you have any business logic which makes use of array size, then you’ll have hard time rectifying and debugging the code. It’ll take unnecessary time and effort to make it work correctly once again.
By making use of Macros or constant variables you can handle this very efficiently. You can make the change at one place and it’ll take effect at all the places in your source code.
Watch the video below for demonstration of effectiveness of using macros and constants for assigning size of an array.
Video Tutorial: Assign Size of Array using Macro and Constant: C Program
Source Code: Size of Array using Macro and Constant: C Program
Without using Macros and/or constants
#include<stdio.h>
int main()
{
int a[5], i;
printf("Enter 5 integer numbers\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 1 2 3 4 5
Here we have a array variable with size 5. We enter 5 integer variables and we display those elements using for loop. In for loop condition we mention the number of times it has to iterate.
Now assume that the requirement changes and we need to increase the size of array from 5 to 7:
#include<stdio.h>
int main()
{
int a[7], i;
printf("Enter 7 integer numbers\n");
for(i = 0; i < 7; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < 7; i++)
printf("%d\n", a[i]);
return 0;
}
As you can see we made edits at 4 places. Its a very simple program and even in that we had to make 4 edits. What if the program is huge and the requirement changes?
Macros
Assign Array size using Macros
#include<stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < N; i++)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 1 2 3 4 5
Observe the changes we’ve made in the source code. We’re defining a macro here. Macro template is N and macro expansion is 5. We replace the value 5 inside main method by macro name N.
#include<stdio.h>
#define N 7
int main()
{
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < N; i++)
printf("%d\n", a[i]);
return 0;
}
Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the macro expansion from 5 to 7, and it starts working as intended.
Constants
Assign Array size using Constants
#include<stdio.h>
int main()
{
const int N = 5;
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < N; i++)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 1 2 3 4 5
Observe the changes we’ve made in the source code. We’ve declared and initialized a constant variable N. Constant variable name is N and its value is 5. We replace the value 5 inside main method by constant variable N.
#include<stdio.h>
int main()
{
const int N = 7;
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 0; i < N; i++)
printf("%d\n", a[i]);
return 0;
}
Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the value of constant variable N from 5 to 7, and the program works as intended.
Note: It’s always considered best practice to either use macros or constant variables to assign array size. This practice will prove to be very advantageous while writing big programs.
Lets look at basics of arrays in C programming language. We’ve already covered a lot of stuffs about arrays in Introduction To Arrays: C Programming Language. In this video tutorial we’ll look at some specific things about arrays which we use often.
Each cell in array occupies memory space depending upon its data type. Int and float occupies 4 bytes. Char occupies 1 byte. Double occupies 8 bytes. Also remember, size of data type is machine dependent.
Initializing and Accessing Array Elements
#include<stdio.h>
int main()
{
int a[5], i;
printf("Enter 5 integer numbers\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
printf("Array elements are: \n");
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 1 2 3 4 5
Arrays is one of the most important topics in C programming language. In this video tutorial I’ll give you a brief introduction to Arrays.
Declaring Normal/regular Variable
Syntax:
Data_type variable_name;
Ex: int a;
Declaring Array Variable
Syntax:
Data_type variable_name[array_size];
Ex: int a[5];
Here array variable is a, it can hold upto 5 integer values.
Definition of Array
An array is a collection of data items, of same data type, accessed using a common name.
Important Notes About Arrays In C
1. All the elements inside an array MUST be of same data type. 2. If you try to enter more elements than the size allocated to the array, it’ll start throwing error. 3. If you input less number of elements than the size of array, then remaining memory blocks will be filled with zeros. 4. Array variable name(without index) holds the base address or the address of first element of the array. 5. Previous address plus the size of the data type of the array gives the address of next element in the array.
Source Code: Introduction To Arrays: C Programming Language
Array Read Write: integer type array
#include<stdio.h>
int main()
{
int a[5], i;
printf("Enter 5 integers\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &a[i]);
}
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Enter 5 integers 6 8 5 9 2 Array elements are: 6 8 5 9 2
Declaring and Initializing: integer type array
#include<stdio.h>
int main()
{
int a[5] = { 4, 5, 1, 9, 2 }, i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 1 9 2
Since a[5] is of type integer, all the array elements must be integers too. we must enclose all the elements inside curly braces and each element must be separated by a comma.
Trying to insert more values than array size
#include<stdio.h>
int main()
{
int a[5] = { 4, 5, 1, 9, 2, 6 }, i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: warning: excess elements in array initializer.
In above source code we are trying to insert 6 integer values inside a[5], which can hold only 5 integer numbers. Hence compiler throws error and stops further compilation.
Inserting less elements/values than array size
#include<stdio.h>
int main()
{
int a[5] = { 4, 5, 1 }, i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 1 0 0
Here array size is 5, but we’re only initializing 3 integer values. So rest of it will be filled with zeros.
To avoid conflict between number of elements and array size
#include<stdio.h>
int main()
{
int a[] = { 4, 5, 2, 6, 1, 2, 4, 5 }, i;
printf("Array elements are:\n");
for(i = 0; i < 8; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 2 6 1 2 4 5
Here we’re not specifying the size of array variable a. Compiler dynamically allocates size to it based on the number of integer numbers assigned to it.
Another method of assigning values to array variable
#include<stdio.h>
int main()
{
int a[3], i;
a[0] = 4;
a[1] = 5;
a[2] = 9;
printf("Array elements are:\n");
for(i = 0; i < 3; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 9
We could use the index and insert the value at specified position inside an array.
Note: Indexing starts from 0 in C programming language. For example, if you have an array a[5], then the elements are accessed one by one like this: a[0], a[1], a[2], a[3], a[4].
Overwriting value of elements in an array
#include<stdio.h>
int main()
{
int a[] = { 4, 5, 2, 6, 1, 2, 4, 5 }, i;
a[5] = 100;
printf("Array elements are:\n");
for(i = 0; i < 8; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 2 6 1 100 4 5
Here previous value of a[5], which is 2 will be replaced by 100.
Here variable a will have base address or the address of first array element. In above program we’re printing the value of a and also the address where the first element of the array is stored. Both display the same address, meaning: a has base address or the address of first element in the array.
If you observe above addresses, there is a difference of 4 between each address. That’s because each memory cell stores integer type data(in above program), which is allocated with 4 bytes of memory(it is machine dependent).
Characters and Arrays
#include<stdio.h>
int main()
{
char ch[5] = { 'A', 'P', 'P', 'L', 'E' };
int i;
for(i = 0; i < 5; i++)
printf("%c", ch[i]);
return 0;
}
Array of characters is called as string. Observe the output of above program. It has ampersand symbol at the end. To remove this kind of random symbols we need to let the program know the end of a string.
Character type data has 1 byte of allocated memory. Since this array stores characters in each cell, the address of consecutive element is 1 byte apart.
Problem Statement: Write macro definitions with arguments for calculation of Simple Interest and Amount.
Store these macro definitions in a file called “interest.h”. Include this file in your program, and use the macro definitions for calculating simple interest and amount.
Output: Enter principal amount 1000 Enter Rate of Interest 9.2 Enter Time Period 2 Simple Interest: 184.00 Total Amount: 1184.00
In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Simple Interest for those values and also the total amount accumulated after getting simple interest.
Note: In Simple Interest formula we are dividing by 100.0 because the ( Principal_amount * Rate_of_interest * Time ) might yield a floating / double type value, so if we divide it by integer 100 then it might give wrong result.