0% found this document useful (0 votes)
4 views

Report4 Array

Uploaded by

akumakirito2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Report4 Array

Uploaded by

akumakirito2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1 . Write a program in C to print all unique elements in an array.

Sample Input :
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5

Sample Output :
The unique elements found in the array are:
3 5

#include <stdio.h>

int main() {
int n, x, y;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);

int a[n];
printf("Input %d elements in the array:\n", n);
for (x = 0; x < n; x++) {
printf("element -%d : ",x);
scanf("%d", &a[x]);
}

printf("The unique elements found in the array are:\n");


for (x = 0; x < n; x++) {
int flag = 1;
for (y = 0; y < n; y++) {
if (x != y && a[x] == a[y]) {
flag = 0;
break;
}
}
if (flag) {
printf("%d ", a[x]);
}
}
printf("\n");

return 0;
}

INPUT & OUTPUT


2.Write a program in C to merge two arrays of the same size sorted in descending order.
Sample Input :
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3

Sample Output:
The merged array in decending order is :
332211

#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the first array: ");
scanf("%d", &n);

int a1[n], a2[n];


printf("Input %d elements in the array:\n", n);

for (int x = 0; x < n; x++) {


printf("element - %d : ", x);
scanf("%d", &a1[x]);
}
printf("Input the number of elements to be stored in the second array: %d\n", n);
printf("Input %d elements in the array:\n", n);

for (int x = 0; x < n; x++) {


printf("element - %d : ", x);
scanf("%d", &a2[x]);
}
int merged[2 * n];
int x = n - 1, y = n - 1, z = 0;
while (x >= 0 && y >= 0) {
if (a1[x] >= a2[y]) {
merged[z++] = a1[x--];
} else {
merged[z++] = a2[y--];
}
}

while (x >= 0) {
merged[z++] = a1[x--];
}

while (y >= 0) {
merged[z++] = a2[y--];
}
printf("The merged array in descending order is:\n");
for (int x = 0; x < 2 * n; x++) {
printf("%d ", merged[x]);
}
return 0;
}

INPUT & OUTPUT


3.Write a program in C to sort elements of an array in descending order.
Sample Input:
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9

Sample Output:
Elements of array in sorted descending order:
97542

#include <stdio.h>

int main() {
int n, x, y, de;
printf("Input the size of array:");
scanf("%d", &n);

int a[n];
printf("Input %d elements in the array:\n", n);
for (x = 0; x < n; x++) {
printf("element -%d : ",x);
scanf("%d", &a[x]);
}
for(x = 0; x < n-1; x++){
for (y = x+1; y < n; y++){
if (a[x] < a[y]){
de = a[x];
a[x] = a[y];
a[y] = de;
}
}
}
printf(" Elements of array in shorted descending order:\n");
for ( x = 0; x < n; x++){
printf("%d ", a[x]);
}
printf("\n");

return 0;
}

INPUT & OUTPUT


4.Write a program in C to find the second largest element in an array.
Sample Input:
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 9
element - 2 : 1
element - 3 : 4
element - 4 : 6
Sample Output:
The Second largest element in the array is : 6

#include <stdio.h>
int main() {
int n, x, y, large, sec_large;
printf("Input the size of array: ");
scanf("%d", &n);

int a[n];
printf("Input %d elements in the array:\n", n);
for (x = 0; x < n; x++) {
printf("element -%d : ",x);
scanf("%d", &a[x]);
}
large = a[0];
for (x =1; x < n; x++){
if (a[x]> large){
large=a[x];
}
}
sec_large = a[0];
for (x = 1; x < n; x++){
if(a[x] > sec_large && a[x] != large){
sec_large = a[x];
}
}

printf("The second largest element in the array is : %d\n",sec_large);

return 0;}
INPUT & OUTPUT
5.Write a program in C to replace the spaces in a string with a specific character.

Sample Input:
Input a string : Be glad to see the back of
Input replace character : *

Sample Output:

After replacing the space with * the new string is :


Be*glad*to*see*the*back*of

Source Code
#include <stdio.h>
int main()
{

char str[60], rep;


printf("Input a string : ");
gets(str);
printf("Input replace character : ");
scanf("%c", &rep);
for( int i=0;str[i] != '\0';i++){
if(str[i]== ' ')
str[i] = rep;
}
puts(s);

return 0;
}

INPUT & OUTPUT


6.Write a program in C to check whether a letter is lowercase or not without any library
function.

Sample Input:
Input a character : w

Sample Output:

The entered letter is a lowercase letter.

#include <stdio.h>

int main() {

char ch;

scanf(" %c", &ch);

if (n >= ‘a’ && n <= ‘z’) {

printf("The entered letter is a lowercase letter\n");

} else {

printf("The entered letter is not a lowercase letter\n");

return 0;

INPUT & OUTPUT


7.Write a program in C to check whether a character is a digit or not without any library
function.

Sample Input:
Input a character : 8

Sample Output:

The entered character is a digit.


#include <stdio.h>

int main() {
char x;
printf("Input a character: ");
scanf(" %c", &x);

if (x >= '0' && x <= '9') {


printf("The entered character is a digit.\n");
} else {
printf("The entered character is not a digit.\n");
}

return 0;
}

INPUT & OUTPUT


8.Write a C program to convert vowels into uppercase characters in a string.

Sample Input:

Input a string : biomedical engineering

Sample Output:

bIOmEdIcAl EngInEErIng

#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
printf("Input a string: ");
gets(str);

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
str[i] = toupper(str[i]);
}

printf("Output string: ");


puts(str);

return 0;
}

INPUT & OUTPUT

You might also like