A2 IPS Arrays
A2 IPS Arrays
(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;
scanf("%d",&n);
for(i=1;i<=n;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;
scanf("%d",&n);
for(i=1;i<=n;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 xi1 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;
scanf("%d", &n);
scanf("%d", &arr[i]);
printf(" %d ",arr[i]);
return 0;
Output:
5. Write a C program that outputs the smallest ‘i’ such that xiis 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 xiand xi+1are 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;
Swap(arr, &arr[j]);
Swap(arr, &arr[j]);
}
printf("\n");
int main()
int n = 7;
bubbleSortWithSwap(arr, 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>
int main(void)
{
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;
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: