0% found this document useful (0 votes)
28 views7 pages

OOPS

Uploaded by

Madhav Santhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

OOPS

Uploaded by

Madhav Santhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Consider an application that allows the user to enter 5 integer values, which
includes both positive and negative numbers. The application will display the
counts the number of positive numbers and negative numbers in the application.
Use C Programming.
CODE:

#include<stdio.h>

void main()

int i, arr[100],pn=0,nn=0;

printf("Enter 5 numbers:");

for(i=0;i<5;i++)

scanf("%d",&arr[i]);

if (arr[i]>=0)

pn++;

else

nn++;

printf("\nPositive numbers:%d",pn);

printf("\nNegative numbers:%d",nn);

OUTPUT:
2. Imagine you are tasked with organizing a programming competition where
participants need to create a C program. The challenge involves creating two
arrays, each with a minimum of 4 elements. The first array
should remain unchanged, while the second array needs to be reversed.
Participants must then merge these two arrays, sort the resulting array, and
finally, print the sorted array.
Implement the solution by passing the arrays to appropriate functions.
CODE:

#include <stdio.h>

#include <stdlib.h>

void mergeAndSort(int arr1[], int arr2[], int size1, int size2) {

int mergedSize = size1 + size2;

int *mergedArray = (int *)malloc(mergedSize * sizeof(int));

for (int i = 0; i < size1; i++) {

mergedArray[i] = arr1[i];

for (int i = 0; i < size2; i++) {

mergedArray[size1 + i] = arr2[i];

for (int i = 0; i < mergedSize - 1; i++) {

for (int j = 0; j < mergedSize - i - 1; j++) {

if (mergedArray[j] > mergedArray[j + 1]) {

int temp = mergedArray[j];

mergedArray[j] = mergedArray[j + 1];

mergedArray[j + 1] = temp;

printf("The sorted elements are: ");

for (int i = 0; i < mergedSize; i++) {

printf("%d ", mergedArray[i]);

}
printf("\n");

free(mergedArray);

int main() {

int size1, size2;

printf("Enter the number of elements for First Array: ");

scanf("%d", &size1);

int arr1[size1];

printf("Enter the elements for First Array: ");

for (int i = 0; i < size1; i++) {

scanf("%d", &arr1[i]);

printf("Enter the number of elements for Second Array: ");

scanf("%d", &size2);

int arr2[size2];

printf("Enter the elements for Second Array: ");

for (int i = 0; i < size2; i++) {

scanf("%d", &arr2[i]);

printf("Elements After Merging: ");

for (int i = 0; i < size1; i++) {

printf("%d ", arr1[i]);

for (int i = size2 - 1; i >= 0; i--) {

printf("%d ", arr2[i]);

printf("\n");

mergeAndSort(arr1, arr2, size1, size2);

return 0;

}
OUTPUT:

3. Develop a C program for a sports analytics application. The application stores the
performance scores of athletes in an array. You need to write a function “Score”
that takes as input an array of integers representing the athletes' scores and the
size of the array. The function “Score” should return a pointer to the
maximum score in the array.

CODE:

#include <stdio.h>

int *Score(int scores[], int size) {

if (size <= 0) {

return NULL;

int *maxScorePtr = &scores[0];

for (int i = 1; i < size; i++) {

if (scores[i] > *maxScorePtr) {

maxScorePtr = &scores[i];

return maxScorePtr;

int main() {

int scores[] = {90, 85, 95, 88, 92};

int size = sizeof(scores) / sizeof(scores[0]);

int *maxScorePtr = Score(scores, size);

if (maxScorePtr != NULL) {
printf("The maximum score is: %d\n", *maxScorePtr);

} else {

printf("Invalid array size.\n");

return 0;

OUTPUT:

4. Develop a C program to enable the swapping of two numbers. The objective is to


create a function “swap” that utilizes a temporary variable and pointer-based
approach for exchanging the values of two given integers.

CODE:

#include<stdio.h>

void swap(int *a, int *b)

int temp=0;

temp=*a;

*a=*b;

*b=temp;

void main()

int i,j;

printf("Enter A and B values respectively:");

scanf("%d%d",&i,&j);

swap(&i,&j);

printf("A:%d\nB:%d",i,j);

}
OUTPUT:

5. Write a C program to remove duplicate numbers using function & pointer

CODE:

#include <stdio.h>

int removeDuplicates(int *arr, int size) {

if (size == 0 || size == 1) {

return size;

int *current = arr;

for (int i = 1; i < size; i++) {

if (*(current) != *(arr + i)) {

*(++current) = *(arr + i);

*(++current) = '\0';

return current - arr;

int main() {

int arr[100], size;

printf("Enter the size of the array: ");

scanf("%d", &size);

printf("Enter the elements of the array: ");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

int newSize = removeDuplicates(arr, size);


printf("Array after removing duplicates: ");

for (int i = 0; i < newSize; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:

You might also like