Source Code: C Program To Check Repetition of Digit In A Number using Arrays
#include<stdio.h>
int main()
{
int a[10] = {0}, num, rem;
printf("Enter a positive number\n");
scanf("%d", &num);
while(num)
{
rem = num % 10;
if(a[rem] == 1)
break;
else
a[rem] = 1;
num = num / 10;
}
if(num)
printf("There are repetition of digits in the number\n");
else
printf("There are no repetition of digits in the number\n");
return 0;
}
Output 1: Enter a positive number 123 There are no repetition of digits in the number
Output 2: Enter a positive number 156 There are no repetition of digits in the number
Output 3: Enter a positive number 1232 There are repetition of digits in the number
Logic To Check if any digit in user input number repeats or not
1. Since there are 10 digits i.e., 0 to 9 to form any number, we take array size as 10. We initialize all the elements of array to 0.
4. By modulo dividing user input number by 10, we fetch individual digits of number. We make use of this individual digit as index of the array. We over-write the initial value(which is zero) and assign 1 at that index position.
So presence of value 1 at an index specifies that the digit already exists in the number.
In this video tutorial lets learn about Designated Initializers in arrays.
C99 introduced the concept of designated initializers. These allow you to specify which elements of an array, structure or union are to be initialized by the values following.
We’ll look at using Designated Initializers for structures and unions in separate video tutorial, for now lets see how we can use it for arrays in C programming language.
When you have a very large array and most of the elements of array are zeros and only couple of elements are non-zeros. In that case, you can use designators to initialize particular elements of an array to non-zero values.
Video Tutorial: Designated Initializers In Array: C Program
Source Code: Designated Initializers In Array: C Program
1. Display array Elements
#include<stdio.h>
int main()
{
int a[5] = {0, 0, 1, 0, 8}, i;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 0 0 1 0 8
We use simple for loop to loop through all the elements of an array and display it on to the console window.
2. Garbage values inside uninitialized array
#include<stdio.h>
int main()
{
int a[5], i;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 6356864 4200750 4200656 46 8
If array elements are not initialized it’ll have garbage values. Even if you initialize one element in that array, the rest of the elements will be automatically initialized to zero. And even if you simply have a equal sign and opening and closing curly braces in front of array variable, the compiler will assign zero to all the elements.
3. All zeros as array element
#include<stdio.h>
int main()
{
int a[5] = {}, i;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 0 0 0 0 0
Since we’ve opening and closing flower bracket or curly brackets in front of array declaration, compiler will automatically assign zeros as array elements.
4. Overwrite the values of array element
#include<stdio.h>
int main()
{
int a[5], i;
a[2] = 1;
a[4] = 8;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 6356864 4200766 1 46 8
From source code 2 above, we know that uninitialized arrays will have garbage values inside it. We are over-writing the garbage values at index 2 and index 4 with 1 and 8 respectively. So except for index 2 and 4 all other spots are filled with garbage values.
So this fixes our issue. Since we’ve empty opening and closing curly braces after declaring array variable, all the elements of array will be automatically initialized to 0.
After that we overwrite the values at index 2 and 4 with 1 and 8.
Now lets make use of Designated Initializers
For arrays with large size we need to use Designated Initializers. Because we can’t individually assign values by overwriting it. It’ll take lot of space in your source code. To avoid that, and to write it in single line of code, we make use of Designated Initializers.
As you can see the designator [4] appears before the designator [2], but still it doesn’t change anything with the output.
8. Designated Initializers: Alternate Syntax
#include<stdio.h>
int main()
{
// {0, 0, 1, 0, 8}
int a[5] = {[2]1, [4]8}, i;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 0 0 1 0 8
As you can see in above source code, we are not using equal sign(=) to assign value to designators. And it’s valid syntax.
9. Designated Initializers: Mixed Syntax
#include<stdio.h>
int main()
{
// {0, 0, 1, 0, 8}
int a[5] = {[2]1, [4] = 8}, i;
for(i = 0; i < 5; i++)
printf("%d\n", a[i]);
return 0;
}
Output: 0 0 1 0 8
You can see that we’ve used both syntax to initialize value to particular index of the array. i.e., [2]1 and [4] = 8. Even if we mix both these syntax, it works and its perfectly valid syntax in C standard.
10. Designated Initializers: And dynamic array size
If we don’t mention length or size of the array explicitly, then the compiler will assign the length or size of the array from the largest designator in the list.
Note: Since the largest designator is 10 in above source code, which must be N – 1 i.e, 11 – 1 = 10. So array size is 11. OR since index starts from 0, and the largest designator is 10. i.e., 0 to 10, which means 11 elements in an array.
11. Designated Initializers: Designators and normal initializers
Here we make use of designators to initialize range of elements. Syntax is [first … last] = value. i.e., the first index to start initializing and the last index to end the initialization. In between these two indexes there are 3 dots.
13. Designated Initializers: initialize range of elements in an array, with mixed syntax
In above source code you can see mixed syntax to initialize elements of the array. i.e., [1 … 3] = 2 is valid, [4 … 6]3 is valid too, and index 7 is assigned a value of 5 directly. All other indexes will have a value of zero.
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