The document provides solutions to various problems involving 1D arrays in C and Python. It includes programs to:
1. Reverse the elements of an array
2. Determine if an array contains all even, odd, or mixed elements
3. Count the number of even and odd elements in an array
4. Check if two arrays are equal
5. Find the sum of perfect square elements in an array
6. Find the minimum scalar product of two vectors
7. Find the smallest and largest elements in an array
8. Print all distinct elements in an array
9. Check if two arrays are disjoint
For each problem, it provides the algorithm, C and Python solutions, sample input/output,
This document contains some programs of C using Data structures, like Stack, LinkedList, queue, Fibonacci series, addition and multiplication of two matrices,etc.
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
All rights reserved. No part of this slide
may be reproduced, stored in a retrieval
system, or transmitted, in any form or by
any means, without the prior permission.
1sequences and sampling. Suppose we went to sample the x-axis from X.pdfrushabhshah600
1sequences and sampling. Suppose we went to sample the x-axis from Xmin to Xmax using a
step size of step
A)Draw a picture of what is going on.
B) Write a expression for n the total number of samples involved (in terms of Xmin, Xmax and
step)
C) Write out the sequence of x-samples
D) Write a direct and general expression for xi that captures the sequence
E) Write a recursive expression for the sequence
F) Write a program to compute and store the x-samples over the range -5x5 using a step size of
0.1 do everything in main ()
2 . We talked about the following string functions that are available in C (as long as you include
string.h):
int strlen(char str[])
void strcpy(char str1[], char str2[])
void strcat(char str1[], str2[])
Write your own versions of these functions; for example: int paul_strlen(int char str[]). Hint: for
your version of the strlen function, start at the first character in the array and keep counting until
you find the ‘\\0’ character (use a while loop for this). Note: Use your version of the strlen
function in the strcpy and strcat functions.
9. We want to insert a number into an array.
(a) Formulate the problem mathematically with two sequences: x and y. (b) Write a function of
the form:
insertNumIntoArray(int n, int array[], int num, int index)
The function inserts num into the array at the specified index. The rest of the array then follows.
For example, if num = 9 and index = 3 and array = [7 2 8 8 3 1 2] then the function will produce:
array = [7 2 8 9 8 3 1 2]
Note: assume that array is properly dimensioned to have at least 1 extra space for storage.
10. Repeat #2 by for the delete operation; that is, we want to delete a single element (at a
specified index) from an array; for example, suppose index = 3 and array = [50 70 10 90 60 20],
then the result will be
array: [50 70 10 60 20]
11. Repeat #2 by for an insert operation where we are inserting several values into the array. The
function should be of the form:
int insertArrayIntoArray(int n, int inArray[],
int nInsert, int insertArray[], int outArray[], int index)
The dimension of outArray is returned (explicitly). For example:
inArrayarray: [7 2 8 6 3 9]
insertArray: [50 60 70]
index: 2
outArray: [7 2 50 60 70 8 6 3 9]
Assume that outArray is large enough to hold all n + nInsert values.
Solution
#include
//Simulates strlen() library function
int paul_strlen(char str[])
{
int l;
for(l = 0; str[l] != \'\\0\'; l++) ;
return l;
}
//Simulates strcpy() library function
void paul_strcpy(char str1[], char str2[])
{
int c;
for(c = 0; str1[c] != \'\\0\'; c++)
str2[c] = str1[c];
str2[c] = \'\\0\';
printf(\"\ Original String: %s\", str1);
printf(\"\ Copied String: %s\", str2);
}
//Simulates strcat() library function
void paul_strcat(char str1[], char str2[])
{
int i, j;
for(i = 0; str1[i] != \'\\0\'; i++) ;
for (j = 0; str2[j] != \'\\0\'; i++, j++)
{
str1[i] = str2[j];
}
str1[i] = \'\\0\';
printf(\"\ Concatenated String: %s\", str1);
}
int main()
{
char data1[20], data2[20];
pri.
These problems are so common that you will find in any C learning curriculum. Either in your college or in any IT institute.
I have provided solutions to these problems as well.
Happy learning...
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
This document contains some programs of C using Data structures, like Stack, LinkedList, queue, Fibonacci series, addition and multiplication of two matrices,etc.
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
All rights reserved. No part of this slide
may be reproduced, stored in a retrieval
system, or transmitted, in any form or by
any means, without the prior permission.
1sequences and sampling. Suppose we went to sample the x-axis from X.pdfrushabhshah600
1sequences and sampling. Suppose we went to sample the x-axis from Xmin to Xmax using a
step size of step
A)Draw a picture of what is going on.
B) Write a expression for n the total number of samples involved (in terms of Xmin, Xmax and
step)
C) Write out the sequence of x-samples
D) Write a direct and general expression for xi that captures the sequence
E) Write a recursive expression for the sequence
F) Write a program to compute and store the x-samples over the range -5x5 using a step size of
0.1 do everything in main ()
2 . We talked about the following string functions that are available in C (as long as you include
string.h):
int strlen(char str[])
void strcpy(char str1[], char str2[])
void strcat(char str1[], str2[])
Write your own versions of these functions; for example: int paul_strlen(int char str[]). Hint: for
your version of the strlen function, start at the first character in the array and keep counting until
you find the ‘\\0’ character (use a while loop for this). Note: Use your version of the strlen
function in the strcpy and strcat functions.
9. We want to insert a number into an array.
(a) Formulate the problem mathematically with two sequences: x and y. (b) Write a function of
the form:
insertNumIntoArray(int n, int array[], int num, int index)
The function inserts num into the array at the specified index. The rest of the array then follows.
For example, if num = 9 and index = 3 and array = [7 2 8 8 3 1 2] then the function will produce:
array = [7 2 8 9 8 3 1 2]
Note: assume that array is properly dimensioned to have at least 1 extra space for storage.
10. Repeat #2 by for the delete operation; that is, we want to delete a single element (at a
specified index) from an array; for example, suppose index = 3 and array = [50 70 10 90 60 20],
then the result will be
array: [50 70 10 60 20]
11. Repeat #2 by for an insert operation where we are inserting several values into the array. The
function should be of the form:
int insertArrayIntoArray(int n, int inArray[],
int nInsert, int insertArray[], int outArray[], int index)
The dimension of outArray is returned (explicitly). For example:
inArrayarray: [7 2 8 6 3 9]
insertArray: [50 60 70]
index: 2
outArray: [7 2 50 60 70 8 6 3 9]
Assume that outArray is large enough to hold all n + nInsert values.
Solution
#include
//Simulates strlen() library function
int paul_strlen(char str[])
{
int l;
for(l = 0; str[l] != \'\\0\'; l++) ;
return l;
}
//Simulates strcpy() library function
void paul_strcpy(char str1[], char str2[])
{
int c;
for(c = 0; str1[c] != \'\\0\'; c++)
str2[c] = str1[c];
str2[c] = \'\\0\';
printf(\"\ Original String: %s\", str1);
printf(\"\ Copied String: %s\", str2);
}
//Simulates strcat() library function
void paul_strcat(char str1[], char str2[])
{
int i, j;
for(i = 0; str1[i] != \'\\0\'; i++) ;
for (j = 0; str2[j] != \'\\0\'; i++, j++)
{
str1[i] = str2[j];
}
str1[i] = \'\\0\';
printf(\"\ Concatenated String: %s\", str1);
}
int main()
{
char data1[20], data2[20];
pri.
These problems are so common that you will find in any C learning curriculum. Either in your college or in any IT institute.
I have provided solutions to these problems as well.
Happy learning...
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
How to Split Bills in the Odoo 17 POS ModuleCeline George
Bills have a main role in point of sale procedure. It will help to track sales, handling payments and giving receipts to customers. Bill splitting also has an important role in POS. For example, If some friends come together for dinner and if they want to divide the bill then it is possible by POS bill splitting. This slide will show how to split bills in odoo 17 POS.
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
Andreas Schleicher presents at the OECD webinar ‘Digital devices in schools: detrimental distraction or secret to success?’ on 27 May 2024. The presentation was based on findings from PISA 2022 results and the webinar helped launch the PISA in Focus ‘Managing screen time: How to protect and equip students against distraction’ https://www.oecd-ilibrary.org/education/managing-screen-time_7c225af4-en and the OECD Education Policy Perspective ‘Students, digital devices and success’ can be found here - https://oe.cd/il/5yV
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
Ethnobotany and Ethnopharmacology:
Ethnobotany in herbal drug evaluation,
Impact of Ethnobotany in traditional medicine,
New development in herbals,
Bio-prospecting tools for drug discovery,
Role of Ethnopharmacology in drug evaluation,
Reverse Pharmacology.
How to Create Map Views in the Odoo 17 ERPCeline George
The map views are useful for providing a geographical representation of data. They allow users to visualize and analyze the data in a more intuitive manner.
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
Thesis Statement for students diagnonsed withADHD.ppt
programs on arrays.pdf
1. Solve the given problems on arrays
1D ARRAY:
1. Program to reverse the elements of an array
Input:
arr = {1, 2, 3, 4, 5}
Output:
Reversed array : {5, 4, 3, 2, 1}
Algorithm to reverse an array
Input the number of elements of an array.
Input the array elements.
Traverse the array from the last.
Print all the elements.
Solution using C
#include<stdio.h>
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
printf(“Reversed array is:”);
for(i = n-1; i >= 0; i–)
printf(“%dt”,arr[i]);
return 0;
}
Solution using python3
n = int(input())
sum = 0
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
print("Reversed array is :",end=" ")
arr.reverse()
for i in range(0,n):
print(arr[i],end=" ")
Output:
3
3
2
1
Reversed array is : 1 2 3
2. Program to find the array type (even, odd or mixed array)
Sample Input:
5
2
4
1
3
5
Sample output:
Mixed
2. Algorithm to find the array type (even, odd or mixed array)
Input the number of elements of the array.
Input the array elements.
If all the elements of the array are odd, display "Odd".
If all the elements of the array are even, display "Even".
Else, display "Mixed".
Solution using C
#include<stdio.h>
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
int odd = 0, even = 0;
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
for(i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
odd++;
if(arr[i] % 2 == 0)
even++;
}
if(odd == n)
printf(“Odd”);
else if(even == n)
printf(“Even”);
else
printf(“Mixed”);
return 0;
}
Solution using python3
# Python program find the array type (even, odd or
mixed array)
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
odd = 0
even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
odd = odd + 1
else:
even = even + 1
if(odd == n):
print(“Odd”)
elif(even == n):
print(“Even”)
else:
print(“Mixed”);
Output:
5
1 2 3 4 5
Mixed
3. Program to count the number of even and odd elements in an array
Sample Input:
3 (number of elements of the array)
1 (array elements)
2
3
Sample Output:
Odd: 2
Even: 1
3. Algorithm to count the number of even and odd elements in an array
Input the number of elements of the array.
Input the array elements.
Initialize count_odd = count_even = 0.
Traverse the array and increment count_odd if the array element is odd, else increment
count_even.
Print count_odd and count_even.
Solution using C
#include
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
int count_odd =0, count_even = 0;
for( i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
}
printf(“Odd: %d”,count_odd);
printf(“nEven: %d”,count_even);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
count_odd = 0
count_even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
count_odd = count_odd + 1
else:
count_even = count_even + 1
print(“Odd:”, count_odd)
print(“Even:”, count_even)
Output:
5
1 2 3 4 5
Odd : 3
Even : 2
4. Program to check if two arrays are equal or not
Sample Input:
3
3
1
2
3
1
2
3
Sample output:
Same
4. Algorithm to check if two arrays are equal or not
Input the number of elements of arr1 and arr2.
Input the elements of arr1 and arr2.
Sort arr1 and arr2 using any sorting technique
If all the elements of arr1 and arr2 are not equal, then print "not Same".
Else, print " Same".
Solution using C
#include<stdio.h>
int sort(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])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int arrays_equal(int arr1[], int arr2[], int n, int
m)
{
int i;
sort(arr1,n);
sort(arr2,m);
for(i = 0; i < n; i++)
{
if(arr1[i] != arr2[i])
{
return 0;
}
}
return 1;
}
int main()
{
int ,arr[20],n,m,i;
scanf(“%d”,&n);
scanf(“%d”,&m);
for(i = 0; i < n; i++)
scanf(“%d”,&arr1[i]);
for(i = 0; i < m; i++)
scanf(“%d”,&arr2[i]);
if(arrays_equal(arr1, arr2, n, m) == 0)
Solution using python3
def arrays_equal(arr1, arr2, n, m):
arr1.sort()
arr2.sort()
for i in range(0,n):
if(arr1[i] != arr2[i]):
return 0
return 1
n = int(input())
m = int(input())
arr1 = []
arr2 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,m):
temp = int(input())
arr2.append(temp)
if(arrays_equal(arr1, arr2, n, m) == 0):
print(“Not Same”)
else:
print(“Same”)
5. printf(“Not same”);
else
printf(“Same”);
return 0;
}
4. Program to find the sum of perfect square elements in an array
Input: arr = {1, 2, 3, 4, 5, 9}
The perfect squares are 1, 4, 9.
Sum = 1 + 4 + 9 = 14
Output: 14
Algorithm to find the sum of perfect square elements in an array
Input the number of elements of the array.
Input the array elements.
Initialize sum = 0.
Check if the array element is a perfect square.
If it is a perfect square, sum = sum + num.
Return sum
Solution using C
#include<stdio.h>
#include<math.h>
int isPerfectSquare(int number)
{
int iVar;
float fVar;
fVar=sqrt((double)number);
iVar=fVar;
if(iVar==fVar)
return number;
else
return 0;
}
int main()
{
int n,i,arr[20],sum=0;
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
for(i = 0; i < n; i++)
sum = sum + isPerfectSquare(arr[i]);
printf(“%d”,sum);
return 0;
}
Solution using python3
from math import *
def isPerfectSquare(number):
if(sqrt(number) == floor(sqrt(number))):
return number
else:
return 0
n = int(input())
sum = 0
arr1 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,n):
sum = sum + isPerfectSquare(arr1[i])
print(sum)
6. Output:
4
1 4 9 16
30
5. Program to find the minimum scalar product of two vectors (dot product)
Sample Input 1:
3 (Number of elements of the array)
1 3 5 (Array 1 elements)
2 4 1 (Array 2 elements)
Sample Output 1:
15
Calculation:
Minimum scalar product = 1 * 4 + 3 * 2 + 5 * 1
= 4 + 6 + 5
= 15
Algorithm to find the minimum scalar product of two vectors
Input the number of elements of the arrays.
Input the array 1 and array 2 elements.
Initialize sum = 0.
Sort the array 1 in ascending order.
Sort the array 2 in descending order.
Repeat from i = 1 to n
o sum = sum + (arr1[i] * arr2[i])
Return sum.
Solution using C
#include<stdio.h>
int sort(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])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int sort_des(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])
Solution using python3
n = int(input())
arr1 = []
arr2 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,n):
temp = int(input())
arr2.append(temp)
arr1.sort()
arr2.sort(reverse=True)
sum = 0
for i in range(0, n):
sum = sum + (arr1[i] * arr2[i])
print(“Minimum Scalar Product :”,sum)
7. {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int main()
{
int n ,i, arr1[n], arr2[n];;
scanf(“%d”,&n);
for(i = 0; i < n ; i++)
scanf(“%d”,&arr1[i]);
for(i = 0; i < n ; i++)
scanf(“%d”,&arr2[i]);
sort(arr1, n);
sort_des(arr2, n);
int sum = 0;
for(i = 0; i < n ; i++)
sum = sum + (arr1[i] * arr2[i]);
printf(“%d”,sum);
return 0;
}
6. Program to find the smallest and largest elements in an array
For example, consider the array.
arr = {1, 2, 3, 4, 5}
Smallest element : 1
Largest element : 5
Algorithm to find the smallest and largest numbers in an array
Input the array elements.
Initialize small = large = arr[0]
Repeat from i = 1 to n
if(arr[i] > large)
large = arr[i]
if(arr[i] < small)
small = arr[i]
Print small and large.
8. Solution using C
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
scanf(“%d”,&n);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf(“nThe smallest element is
%dn”,small);
printf(“nThe largest element is
%dn”,large);
return 0;
}
Solution using python3
arr = []
n = int(input())
for i in range(0,n):
numbers = int(input())
arr.append(numbers)
print("Maximum element : ", max(arr),
"nMinimum element :" , min(arr))
Output:
5
1 2 3 4 5
The smallest element is 1
The largest element is 5
7. Program to print all the distinct elements in an array
SAMPLE INPUT:
9 = size of an array
2 3 4 5 6 1 2 3 4 = array elements
SAMPLE OUTPUT:
2 3 4 5 61
Algorithm to print distinct numbers in an array
Declare and input the array elements.
Traverse the array from the beginning.
Check if the current element is found in its previous elements the array again.
If it is found, then do not print that element.
Else, print that element and continue.
9. Solution using C
#include <stdio.h>
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
scanf(“%d”, &size_array);
for(i=0; i<size_array; i++)
scanf(“%d”, &arr[i]);
distict_elements(arr, size_array);
return 0;
}
void distict_elements(int a[], int n)
{
int i, j;
for (i=0; i<n; i++)
{
for (j=0; j<i; j++)
{
if (a[i] == a[j])
break;
}
if (i == j)
printf(“%dn “, a[i]);
}
}
Solution using python3
def distinct(arr, num):
arr1 = []
for i in range(0, num):
d = 0
for j in range(0, i):
if (arr[i] == arr[j]):
d = 1
break
if (d == 0):
arr1.append(arr[i])
print(“Distinct elements:”)
print (arr1)
n = int(input())
arr = []
for i in range(n):
arr.append(int(input()))
print(“Input array: “)
print (arr)
distinct(arr, n)
Output:
5
1 1 2 3 4
1
2
3
4
8. Program to check if the given arrays are disjoint
For example :
arr1 = {1,2,3,4,5}
arr2 = {6,7,8,9}
arr1 and arr2 elements are unique and hence they are said to be disjoint.
arr3 = {1,2,3,4,5}
arr4 = {4,5,6,7}
arr3 and arr4 are not disjoint as they have elements 4 and 5 in common.
Input:
10. 4
1 2 3 4
3
6 7 8
given arrays are disjoint
Algorithm to check if the given arrays are disjoint
Use two loops.
Traverse the array 1 using the outer loop.
Use the inner loop to check if the elements in array 2 are found in array 1.
If at least one element of array 2 is found in array 1, return false. Else return true.
Solution using C
#include <stdio.h>
int disjoint_arrays(int arr1[], int arr2[], int n,
int m)
{
int i,j;
for(i = 0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr1[i] == arr2[j])
return -1;
}
}
return 1;
}
int main()
{
int m,n,arr1[20],arr2[20],i,j;
printf("Enter the size of array 1n");
scanf("%d",&n);
printf("Input Array 1 elements n");
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
printf("Enter the size of array 2 n");
scanf("%d",&m);
printf("Input Array 2 elements n");
for(i=0;i<m;i++)
scanf("%d",&arr2[i]);
int res = disjoint_arrays(arr1,arr2,n,m);
if(res == -1)
printf("The arrays are not disjointn");
else
printf("The arrays are disjointn");
return 0;
}
Solution using python3
def disjoint_arrays(arr1,arr2):
for i in range(0,len(arr1)):
for j in range(0,len(arr2)):
if(arr1[i] == arr2[j]):
return -1
return 1;
arr1 = [1,2,3,4,5]
arr2 = [6,7,8,9,10]
res = disjoint_arrays(arr1,arr2)
if(res == -1):
print(“The arrays are not disjoint”)
else:
print(“The arrays are disjoint”)
11. 9. Program to find all symmetric pairs in an array. Two pairs (p,q) and (r,s) are said to be
symmetric when q is equal to r and p is equal to s. For example, (5,10) and (10,5) are symmetric
pairs
For example,
Consider a 2D array,
Input:
arr [6] [2] = {{1, 2}, {3, 4}, {5, 6}, {2, 1}, {4, 3},{10,11}}
Output:
{1,2} and {2,1} are symmetric
{3,4} abd {4,3} are symmetri
This problem can be solved in two different ways.
Method 1: Using two loops, one loop to traverse the pairs and the other loop to check if similar pair is
existing.
Method 2: An efficient way to solve this problem is to use hashing. Insert each array pairs into the hash
table with the first element of the pair as the key and the second element as the value. Traverse the hash
table to check if the pairs are found again.
Algorithm to find all symmetric pairs in an array
Input the array from the user.
1. Use two loops.
2. One loop for traversing the array and the other loop to check if symmetric pair is found in the
array.
3. If symmetric pair is found, print the pairs.
Algorithm to find symmetric pairs in an array using hashing
1. Input the array from the user.
2. From all the array pairs, the first element is used as the key and the second element is used as
the value.
3. Traverse all the pairs one by one.
4. For every pair, check if its second element is found in the hash table.
5. If yes, then compare the first element with the value of the matched entry of the hash table.
6. If the value and the first element match, it is displayed as a symmetric pair.
7. Else, insert the first element as the key and second element as value and repeat the same.
12. 10. Program to insert, delete and search an element in an array
Input format:
Input consists of 3 integers and 1 array.
Input the size of the array.
Input the array elements.
Input the position where the element should be inserted.
Input the element to be inserted in that position.
Sample Input:
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position)
10 (Element to be inserted)
Sample Output:
Array after insertion is:
1
2
3
10
4
5
Algorithm to insert, delete and search an element in an array
Insertion
Input the array elements, the position of the new element to be inserted and the new element.
Insert the new element at that position and shift the rest of the elements to right by one position.
Deletion
Input the array elements, the position of the new element to be inserted and the new element.
Delete the element and shift the rest of the elements to left by one position.
Search
Input the array elements, the element to be searched.
Traverse the array and check if the element is present in the array.
13. Display "Yes" if it is present in the array, else display "No".
Insertion:
Solution using C
#include <stdio.h>
int main()
{
int n,arr[20],i,pos;
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
scanf(“%d”,&pos);
int ele;
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= pos ; i–)
arr[i+1] = arr[i];
arr[pos] = ele;
printf(“Array after insertion
is:n”);
for (i = 0; i <= n; i++)
printf(“%dn”, arr[i]);
}
return 0;
}
Solution using python3
n = int(input())
sum = 0
arr = [1,2,3,4,5]
pos = int(input())
ele = int(input())
arr.insert(pos,ele)
for i in range(0,n):
print(arr[i], end = ” “)
Deletion:
Solution using C
#include <stdio.h>
int main()
{
int array[10], position, c, n;
printf(“Enter the number of elements of
the array : “);
scanf(“%d”, &n);
printf(“nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“nEnter the position : “);
scanf(“%d”, &position);
if (position >= n+1)
printf(“nDeletion not possible.n”);
else
Solution using python3
arr = [1,2,3,4,5]
pos = int(input())
arr.remove(arr[pos])
for i in range(0,n-1):
print(arr[i], end = ” “)
14. {
for (c = position ; c < n – 1; c++)
array[c] = array[c+1];
printf(“nArray after deletion : “);
for (c = 0; c < n – 1; c++)
printf(“%dn”, array[c]);
}
return 0;
}
Searching:
Solution using C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[10], ele, c, n;
printf(“Enter the number of elements of
the array : “);
scanf(“%d”, &n);
printf(“nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“nEnter element : “);
scanf(“%d”, &ele);
for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
printf(“nElement
foundn”);
exit(0);
}
}
printf(“element not foundn”);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
ele = int(input())
for i in range(0,n):
if(arr[i] == ele):
print("Element Found")
exit()
print("element not found")
15. 11. Program to sort the array of elements
Solution using C
#include<stdio.h>
int main()
{
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements
are:n");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("nAfter sorting array elements
are:n");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
print("before sorting")
for i in range(0,n):
print(arr[i],end=" ")
print("nafter sorting")
arr.sort();
for i in range(0,n):
print(arr[i],end=" ")
OUTPUT:
Enter array size:5
Enter 5 elements:
3 4 2 5 1
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5
16. 2D ARRAY:
12. Program to find the maximum element in each row of a matrix
Input:
3 3 (Order of the matrix - number of rows and columns)
1 4 9
3 5 1
2 8 5
Output:
9
5
8
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For row = 0 to n-1
Find the maximum element in the row
Print the maximum element
Solution using C
#include<stdio.h>
void maxi_row(int mat[][], int m, int n)
{
int i = 0, j,max;
for(i=0;i<n;i++)
{
max=0;
for ( j = 0; j < n; j++)
{
if (mat[i][j] > max)
max = mat[i][j];
}
printf("%dn",max);
}
}
int main()
{
int m, n, i, j, mat[20][20];;
scanf("%d %d",&m,&n);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf("%d",&mat[i][j]);
}
maxi_row(mat,m,n);
return 0;
}
Solution using python3
def maxi_row(arr):
no_of_rows = len(arr)
no_of_column = len(arr[0])
for i in range(no_of_rows):
max = 0
for j in range(no_of_column):
if arr[i][j] > max :
max = arr[i][j]
print(max)
mat = [[1,2,3],[4,5,6],[7,8,9]]
maxi_row(mat)
17. 13. Program to find the minimum element in each row of a matrix
Input:
3 3 (Order of the matrix - number of rows and columns)
1 4 9
3 5 1
2 8 5
Output:
1
1
2
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For row = 0 to n-1
Find the minimum element in the row
Print the minimum element
Solution using C
#include<stdio.h>
#include<limits.h>
void min_row(int mat[][20], int m, int n)
{
int i = 0, j,min;
for(i=0;i<n;i++)
{
min = INT_MAX;
for ( j = 0; j < n; j++)
{
if (mat[i][j] < min)
min = mat[i][j];
}
printf("%dn",min);
}
}
int main()
{
int m, n, i, j, mat[20][20];;
scanf("%d %d",&m,&n);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf("%d",&mat[i][j]);
}
min_row(mat,m,n);
return 0;
}
Solution using python3
def maxi_row(arr):
no_of_rows = len(arr)
no_of_column = len(arr[0])
for i in range(no_of_rows):
min = 99999
for j in range(no_of_column):
if arr[i][j] < min :
min = arr[i][j]
print(min)
mat = [[1,2,3],[4,5,6],[7,8,9]]
maxi_row(mat)
18. 14.Program to find transpose of a matrix
Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
1 4 7
2 5 8
3 6 9
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For i = 0 to row_size
o For j= 0 to col_size
Transpose[j][i]=matrix[i][j]
Display the transpose matrix
Solution using C
#include <stdio.h>
int main()
{
int m, n, i, j, matrix[10][10], transpose[10][10];
scanf("%d%d", &m, &n);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%dt", transpose[i][j]);
printf("n");
}
return 0;
}
Solution using python3
matrix=[]
row=int(input("Enter number of rows:"))
column=int(input("Enter number of columns:"))
for i in range(row):
matrix.append([])
for j in range(column):
num=int(input("Enter the element:"))
matrix[i].append(num)
print('n')
print("Input matrix:")
for i in range (row):
for j in range (column):
print (matrix[i][j], end=" ")
print('n')
transpose=[]
for j in range(column):
transpose.append([])
for i in range (row):
t_num=matrix[i][j]
transpose[j].append(t_num)
print('n')
print("Transpose matrix:")
for i in range (row):
for j in range (column):
print (transpose[i][j], end=" ")
print('n')
19. 15. Program to print the sum of boundary elements of a matrix
For example, consider the matrix given below.
1 2 3
4 5 6
7 8 9
Boundary Matrix
1 2 3
4 6
7 8 9
Sum of boundary elements: 1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40
Algorithm to print the sum of boundary elements of a matrix
Input the order of the matrix.
Input the matrix elements.
Print all the boundary elements of the matrix.
Find the sum of the boundary elements.
Print sum
Solution using C
#include <stdio.h>
#include<stdio.h>
int main()
{
int m, n, sum = 0;
printf(“nEnter the order of the matrix : “);
scanf(“%d %d”,&m,&n);
int i, j;
int mat[m][n];
printf(“nInput the matrix elementsn”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}
printf(“nBoundary Matrixn”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
{
printf(“%d “, mat[i][j]);
sum = sum + mat[i][j];
}
else
printf(” “);
}
printf(“n”);
}
printf(“nSum of boundary is %d”, sum);
return 0;
}
Solution using python3
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
m = 3
n = 3
print(“nBoundary Matrixn”)
for i in range (0, m):
for j in range (0, n):
if (i == 0 or j == 0 or i == n – 1 or j
== n – 1):
print(mat[i][j], end = ” “)
sum = sum + mat[i][j]
else:
print(end = ” “)
print(“n”)
print(“nSum of boundary is “, sum)
20. Patterns:
1. Program to print solid square star pattern
SAMPLE INPUT:
5
SAMPLE OUTPUT:
n x n matrix filled with „*‟
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to n-1
Print *
Solution using C
#include <stdio.h>
int main()
{
int i, j,n;
scanf(“%d”,&n);
for(i = 0; i <n; i++)
{
for(j = 0; j < n; j++)
{
printf(“*”);
}
printf(“n”);
}
return 0;
}
Solution using python3
n = int(input())
for i in range(0,n):
for j in range(0,n):
print("*", end = " ")
print()
Output:
5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
21. 2. Program to print Hallow square star pattern
SAMPLE INPUT:
5
SAMPLE OUTPUT:
* * * * *
* *
* *
* *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to n-1
If i = 0 or i=n-1 or j = 0 or j=n-1 then print *
Else print (“ “)
Print(“n”)
Solution using C
#include <stdio.h>
int main()
{
int i, j, n;
scanf(“%d”,&n);
for (i = 0; i <n; i++)
{
for (j = 0; j < n; j++)
{
if (i==0 || i==n-1 || j==0 || j==n-1)
printf(“*”);
else
printf(” “);
}
printf(“n”);
}
return 0;
}
Solution using python3
n = int(input())
for i in range(0, n):
for j in range(0, n):
if (i == 0 or i == n-1 or j == 0 or j == n-1):
print("*", end=" ")
else:
print(" ", end=" ")
print()
Output:
5
* * * * *
* *
* *
* *
* * * * *
22. 3. Program to print half pyramid pattern using stars
SAMPLE INPUT:
5
SAMPLE OUTPUT:
*
* *
* * *
* * * *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to i
Print *
Print(“n”)
Solution using C
#include<stdio.h>
int main()
{
int i, j,n;
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("* ");
}
printf("n");
}
return 0;
}
Solution using python3
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print("*", end =" ")
print()
OUTPUT:
5
*
* *
* * *
* * * *
* * * * *
4. Program to print pyramid pattern printing using numbers
23. SAMPLE INPUT:
5
SAMPLE OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to i
Print j
Print(“n”)
Solution using C
#include<stdio.h>
int main()
{
int i, j,n;
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("n");
}
return 0;
}
Solution using python3
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j, end =" ")
print()
OUTPUT:
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5