One Dimensional Array
One Dimensional Array
Implementation:
Note: This material is for your own
consumption only and should not be #include <stdio.h>
uploaded in the internet, tiktok, facebook, #include <stdlib.h>
youtube and all platforms similar in the int main(int argc, char *argv[])
aforementioned. {
int i, j, k = 0, l = 0, a = 0, count, quot;
Example 1 int rem, prime[100], primefactor[100];
printf(“Enter a number:”);
Prime Factorization – a positive scanf(“%d”,&num);
whole number can be expressed by a for(i=2;i<=100;i++) { //find for the prime
product of two or more prime numbers numbers between 2 - 100
starting from the lowest prime number 2. count = 0;
for(j=2;j<ilj++) {
Illustration: if(count==0) {
15 = 3 * 5 prime[k] = i;
8=2*2*2 k++;
}
Write a program that will ask for a }
number (positive and whole) and determine printf(“The prime factor(s) of %d are: “,num);
the prime factors of this number. // Determine the prime factors
for(;;)
Algorithm: {
quot = num / prime[l];
quot rem rem = num % prime[l];
15 / 2 7 1 if(rem==0) {
15 / 3 5 0 primefactor[a] = prime[l];
5/3 1 2 a++;
5/5 1 0 }
1/5 0 5 else {
l++;
Analysis: }
num = quot;
Divide the number by 2. If the if(quot==0) {
remainder is equal to 0, then the numerator break;
which causes the remainder to be 0 is the }
prime factor of the number. Swap the for(i=0;i<a;i++) {
original number by the quotient and printf(“%d “,primefactor[i]);
continue dividing. If the remainder is not }
equal 0, replace the numerator with the next return 0;
prime number. Continue this process until }
the quotient is 0. If the quotient is 0,
terminate the loop.
Example 2. freq[j] = 0;
}
Write a program that will count the }
occurrence of each element in an array
/* If frequency of current element is not
Illustration: counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}
/*
* Print frequency of each element
*/
Implementation printf("\nFrequency of all elements of array :
\n");
#include <stdio.h> for(i=0; i<size; i++)
#include <stdlib.h> {
int main(int argc, char *argv[]) { if(freq[i] != 0)
{
int arr[100], freq[100]; printf("%d occurs %d times\n", arr[i],
int size, i, j, count; freq[i]);
}
/* Input size of array */ }
printf("Enter size of array: ");
scanf("%d", &size); return 0;
}
/* Input elements in array */
printf("Enter elements in array: "); Example 3.
for(i=0; i<size; i++)
{ Write a program that will remove all
scanf("%d", &arr[i]); duplicate elements in an array.
/*
* Find duplicate elements in array Implementation:
*/
for(i=0; i<size; i++) #include <stdio.h>
{ #include <stdlib.h>
for(j=i+1; j<size; j++) #define MAX_SIZE 100
{
/* If any duplicate found */ /* run this program using the console pauser
if(arr[i] == arr[j]) or add your own getch, system("pause") or
{ input loop */
/* Delete the current duplicate
element */ int main(int argc, char *argv[]) {
for(k=j; k < size - 1; k++) int arr[MAX_SIZE],
{ freq[MAX_SIZE];
arr[k] = arr[k + 1]; int size, i, j, count;
}
/* Input size of array and elements in
/* Decrement size after removing array */
duplicate element */ printf("Enter size of array: ");
size--; scanf("%d", &size);
printf("Enter elements in array: ");
/* If shifting of elements occur for(i=0; i<size; i++)
then don't increment j */ {
j--; scanf("%d", &arr[i]);
} freq[i] = -1;
} }
}
/* Find frequency of each element */
for(i=0; i<size; i++)
/* {
* Print array after deleting duplicate count = 1;
elements for(j=i+1; j<size; j++)
*/ {
if(arr[i] == arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
return 0;
}