0% found this document useful (0 votes)
18 views15 pages

A2 IPS Arrays

The document contains multiple C programming solutions for various problems, including calculating sums, finding local maximums, and checking for even numbers in arrays. Each solution is presented with code snippets and expected outputs. The problems range from basic array manipulations to more complex tasks involving matrix operations.

Uploaded by

abhishekbartan94
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)
18 views15 pages

A2 IPS Arrays

The document contains multiple C programming solutions for various problems, including calculating sums, finding local maximums, and checking for even numbers in arrays. Each solution is presented with code snippets and expected outputs. The problems range from basic array manipulations to more complex tasks involving matrix operations.

Uploaded by

abhishekbartan94
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/ 15

Solution:

(A) Code:

#include<stdio.h>
int main()
{
int arr[200],i,n,sum=0;
printf("Enter the number of elements :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Give %dth number :",i);
scanf("%d",&arr[i]);
}
for(i=1;i<=n-2;i++)
{
sum=sum+(arr[i]+arr[i+1])*arr[i+2];
}
printf("%d",sum);
return 0;
}
Output:

(b)

Code: #include<stdio.h>

int main()

int arr[200],sum=1,i,n;

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

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("Give %dth number:",i);

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

for(i=1;i<=n-2;i++)

{
sum=sum*(arr[i]+arr[i+2]);

printf("%d",sum);

return 0;

Output:
©

Code:

#include<stdio.h>

int main()

int arr[200],sum=0,i,n;

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

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("Give %dth number:",i);

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

for(i=1;i<=n-2;i++)

sum=sum+(arr[i]-arr[i+1])*(arr[i+1]+arr[i+2]);

printf("%d",sum);

return 0;

}
Output:

4. Write a C program, which outputs all local maximums of a given data of elements. A
number xi is a local maximum if it is more than both xi1 and xi+1. If the elements are 25,
19, 22, 23, 21, 12, 10, 17, 11, 13, 10 then 23, 17 and 13 are local maximums.

Solution4:

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

int arr[500], i, n;

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

scanf("%d", &n);

printf("Enter the elements :");

for (i = 0; i < n; i++){

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

printf("The maximums are: ");

for(i = 1; i < n; i++){


if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]){

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

return 0;

Output:

5. Write a C program that outputs the smallest ‘i’ such that xiis even. For example, 22 is
the output for the input 25, 19, 22, 23, 21, 12, 10, 17, 11, 13, 10

Soltion 5:
Code:
#include<stdio.h>
void main()
{
int n,i,j,min;
printf("enter no of elements:");
scanf("%d",&n);
int arr[n];
printf("enter elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
min=i;
break;
}
}
printf("xi=%d\n",arr[i]);
printf("Smallest i is:%d",i);

Output:

6. Write a C program that outputs the smallest ‘i’ such that xiand xi+1are both even. In
above case 6. (Because 12 and 10 are even).

Solution:
Code: #include<stdio.h>
void main()
{
int n,i,j,min;
printf("enter no of elements:");
scanf("%d",&n);
int arr[n];
printf("enter elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
if(arr[i]%2==0&&arr[i+1]%2==0)
{
min=i;
break;
}
}
printf("xi=%d\n",arr[i]);
printf("Smallest i is:%d",i);

Output:
7. Let 𝐴[1 … 𝑛] be an array of 𝑛 integers. Define an operation called 𝑆𝑤𝑎𝑝(𝐴[0], 𝑦), where
the operation swap ony swaps with the first element of the array. Using this swap
operation, re-arrange the elements of the array in such a way that the elements are in non-
decreasing order of their values. Can we show that the parameter 𝐴[0] can be replaced
with 𝐴[𝑖] for any 𝑖, 1 ≤ 𝑖 ≤ 𝑛 and still be able to arrange the elements in non-decreasing
order?
Solution:
Code: #include<stdio.h>
void Swap(int arr[], int *y)
{
int temp = arr[0];

arr[0] = *y;

*y = temp;

}
void bubbleSortWithSwap(int arr[], int n)

int i, j;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

Swap(arr, &arr[j]);

Swap(arr, &arr[j + 1]);

Swap(arr, &arr[j]);
}

void printArray(int arr[], int n)

for (int i = 0; i < n; i++)

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

printf("\n");

int main()

int arr[] = {36, 22, 10, 98, 85, 47, 90};

int n = 7;

bubbleSortWithSwap(arr, n);

printf("Sorted array by using the modified Swap() method: \n");

printArray(arr, n);

return 0;

Output:
8. Let 𝐴[1 … 𝑛] be an array of integers and let 𝑘 be a number. Write a program to check if
the sum of any two numbers in 𝐴 is equal to 𝑘.

Code:

#include <stdio.h>

void findPair(int arr[], int n, int target)


{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] + arr[j] == target)
{
printf("Pair found (%d, %d)\n", arr[i], arr[j]);
return;
}
}
}
printf("Pair not found");
}

int main(void)
{

int arr[] = { 55, 6, 4, 1, 0, 1,12,18 };


int target = 13;

int n = sizeof(arr)/sizeof(arr[0]);

findPair(arr, n, target);

return 0;
}

Output:
9. Given an number 𝑘 write a program to find the number of ‘0’s in 𝑘.

Code:

#include<stdio.h>
int count_digit(int num)
{
static int count=0;
if(num>0)
{

if(num%10==0)
count++;

count_digit(num/10);
}
return count;
}
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
printf("The number of Zeros in the Given Number is %d",count_digit(n));
}

Output
10. Given an number 𝑘 and a digit 𝑑, write a program to
a. check if 𝑑 occurs in 𝑘
b. output the number of times 𝑑 occurs in 𝑘
c. output the exact position(s) at which 𝑑 occurin 𝑘.

Code:

11.Assume that the given matrix is a square matrix. Write program to print oth
the diagonal elements. Optiize on the code to use lesser loop statements,
lesser conditional statements. e.g.
Matrix 5 7 9 4
2 4 7 3
1 5 9 2
3 7 4 8
Output 5 4 9 8
4753
Code:

#include<stdio.h>
int main()
{
int array1[10][10],i,j,m,n,sum = 0;

printf("Enter no. of rows :: ");


scanf("%d", &m);
printf("\nEnter no. of cols :: ");
scanf("%d",&n);
printf("\nEnter values to the matrix :: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter a[%d][%d] value :: ",i,j);
scanf("%d", &array1[i][j]);
}
}

printf("\nThe Diagonals elements of a matrix are :: \n\n");


if(m==n)
{

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

if(i==j)
printf("\t%d", array1[i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}

return 0;
}
Output:

You might also like