Important Questions for Class 12 Computer Science (C++) – Data Structure
Topic – 1
Data Structure and One-Dimensional Array
Previous years Examination Questions
2 Marks Questions
Question 1:
Write the definition of a function AddUp(int Arr[ ], int N) in C++, in which all even positions (i.e. 0,2,4 ) of the array should be added with the content of the element in the next position and odd positions (i.e. 1,3,5, ) elements should be incremented by 10. All India 2017
NOTE
- The function should only alter the content in the same array.
- The function should not copy the altered content in another array.
- The function should not display the altered content of the array.
- Assuming, the Number of elements in the array are Even.
Аnswer:
void AddUp(int Arr[], int N) { for(int i=0;i<N;i++) { if(i%2==0) Arr[i] += Arr[i+1]; else Arr[i] += 10; } }
Question 2:
Write the definition of a function FixPay(float Pay[ ], int N) in C++, which should modify each element of the array Pay having N elements, as per the following rules:
Аnswer:
void FixPay(float Pay[], int N) { for(int i =0;i<=N—1;i++) { if(Pay[i]<100000) Pay[i] = Pay[i]+(Pay[i]*25)/100; else if(Pay[i]>=100000 && Pay[i]<200000) Pay[i] = Pay[i]+(Pay[i]*20)/100; else Pay[i] = Pay[i]+(Pay[i]*15)/100; } }
Question 3:
Write the definition of a function FixSalary(float Salary[ ], int N) in C++, which should modify each element of the array Salary having N elements, as per the following rules:
Аnswer:
void FixSalary(float Salary[], int N) { for(int i=0;i<=N—1;i++) { if(Salary[i]<100000) Salary[i]=Salary[i]+(Salary[i]*35)/100; else if(Salary[i]>=100000 && Salary[i]<200000) Salary[i]=Salary[i]+(Salary[i]*30)/100; else Salary[i]=Salary[i]+(Salary[i]*20)/100; } }
Question 4:
Write the definition of a function Alter (int A[ ], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. e.g. if an array of 10 integers is as follows:
Аnswer:
void Alter(int A[], int N) { for(int i=0;i<10;i++) { if(A[i]%5==0) A[i]=5; else A[i]=0; } }
Question 5:
Write the definition of a function Changeant P[ ], int N) in C++, which should change all the multiples of 10 in the array to 10 and rest of the elements as 1. All India 2015
Аnswer:
void Change(int P[], int N) { for (int i=0;i<N;i++) { if (P[i]%10 == 0) P[i] = 10; else P[i] = 1; } }
Question 6:
Write the definition of a function Modify(int A[ ], int N) in C++, which should reposition the content after swapping each adjacent pair of numbers in it.
[NOTE Assuming the size of array is multiple of 4]
Аnswer:
void Modify(int A[], int N) { for(int i=0;i<N;i++) { int temp = A[i]; A[i] = A[i+2]; A[i+2] = temp; if(i%4>=1) i = i+2; } }
Question 7:
Write code for a function void oddEven(int S[ ], int N) in C++, to add 5 in all the odd values and 10 in all the even values of the array S.
Аnswer:
void oddEven(int S[],int N) { for(int i=0;i<N;i++) { if(S[i]%2 == 0) S[i] += 10; else S[i]+=5; } }
Question 8:
Write code for a function void EvenOdd (int T [ ], int C) in C++, to add 1 in all the odd values and 2 in all the even values of the array T.
Аnswer:
void EvenOdd(int T[],int C) { for(int i=0;i<C;i++) { if(T[i]%2 == 0) T[i] += 2; else T[i] += 1; } }
Question 9:
Write a function in C++ TWOTOONE( ) which accepts two array X[ ], Y[ ] and their size n as argument. Both the arrays X[ ] and Y[ ] have the same number of elements. Transfer the content from two arrays X[ ], Y[ ] to array Z[ ]. The even places (0,2,4….) of array Z[ ] should get the contents from the array X[ ] and odd places (1,3,5…) of array Z[ ] should get the contents from the array Y[ ].
Example: If the X[ ] array contains 30,60,90 and the Y[ ] array contains
10.20.50. Then Z[ ] should contain
30.10.60.20.90.50. All India 2014
Аnswer:
void TW0T00NE(int X[], int Y[],int n) { int Z[40], i,j=0,k=0; for(i= 0;i<(n+n);i++) { if(i%2 == 0) { Z[i] = X[j]; j++; } else { Z[i] = Y[k]; k++; } } }
Question 10:
Write code for a function void Convert (int T[ ], int Num) in C++, which repositions all the elements of the array by shifting each of them one to one position before and by shifting the first element to the last position.
Аnswer:
void Convert(int T[], int Num) { int temp = T[0]; for(int i=0;i<(Num-1);i++) { T[i]=T[i+l]; } T[i]= temp; }
Question 11:
Write code for a function void ChangeOver(int P[ ], int N) in C++, which re-positions all the elements of the array by shifting each of them to the next position and by shifting the last element to the first position.
Аnswer:
void ChangeOver(int P[],int N) { int temp; for(int i=0;i<(N-1);i++) { temp = P[N-1]; P[N-1] = P[i]; P[i] = temp; } }
Question 12:
Write the definition for a function void Transfer (int A[6], int B[6]) in C++, which takes two integer arrays, each containing 6 elements as parameters. The function should exchange all odd places (i.e. 3rd and 5th) of the two arrays.
Аnswer:
void Transfer(int A[6],int B[6]) { int i,temp; for(i=1;i<=5;i=i+2) { temp = A[i]; A[i] = B[i]; B[i] = temp; } }
Question 13:
Write a function SWAP2CHANGE(int p[ ], int N) in C++ to modify the content of the array in such a way that the elements, which are multiples of 10 swap with the value present in the very next position in the array.
e.g. If the content of array p is
91, 50, 54, 22, 30, 54
The content of array p should become
91, 54, 50, 22, 54, 30
Аnswer:
void SWAP2CHANGE(int p[],int N) { int C,i; for(i=0;i<=N-2;i++) { if(p[i]%10 == 0) { C = p[i]; p[i] = p[i+1]; p[i+1] = C; i++: } } }
Question 14:
Write a function SWAP2BEST(int ARR[ ], int Size) in C++ to modify the content of the array in such a way that the elements, which are multiples of 10 swap with the value present in the very next position in the array.
e.g. If the contents of array ARR are
90, 56, 45, 20, 34, 54
The contents of array should become
56, 90, 45, 34, 20, 54 All India 2012
Аnswer:
void SWAP2BEST(int ARR[], int Size) { { int i,C; for(i= 0;i<=Size-2;i++) { if(ARR[i]%10==0) { C=ARR[i]; ARR[i] = ARR[i+1]; ARR[i+1] = C; i++; } } }
Question 15:
Write a Get2From1( ) function in C++ to transfer the content from one array ALL[ ] to two arrays Odd[ ] and Even[ ]. The Even[ ] array should contain the values from places(0, 2, 4, …) of array ALL[ ] and Odd[ ] array should contain the values from odd position like (1, 3, 5, …).
e.g. The ALL[ ] array should contain 30,10, 60, 50, 90, 80
If the Even[ ] array contains 30, 60, 90
and the Odd[ ] array contains
10, 50, 80 All India 2011
Аnswer:
void Get2From1(int ALL[], int N) { int p,q,i,j=0, k=0; if(N%2 == 0) { p = N/2; q = N/2; } else { p = N/2; q = ((N/2)+l); } int *0dd = new int[p]; int *Even = new int[q]; for(i=0;i<(N);i++) { if(i%2 == 0) Even[j++]=ALL[i]; else Odd[k++]=ALL[i]; } }
Question 16:
Write a function CHANGE( ) in C++, which accepts an array of integer and its size as parameters and divide all those array elements by 7, which are divisible by 7 and multiply other array elements by 3.
Аnswer:
void CHANGE(int A[],int N) { for(int i=0;i<N;i++) if(A[i]%7 == 0) A[i]=A[i]/7; else } A[i] = A[i]*3; } }
Question 17:
Write a function REASSIGN( ) in C++, which accepts an array of integer and its size as parameters and divide all those array elements by 5, which are divisible by 5 and multiply other array elements by 2.
Аnswer:
void REASSIGN(int A[],int N) { for(int i=0;i<N;i++) { 1f(A[i]%5 == 0) A[i] = A[i]/5; else A[i] = A[i]*2; } }
Question 18:
Write a function SORTPOINTS( ) in C++ to sort an array of structure Game in descending order points using bubble sort.
NOTE Assume the following definition of structure Game.
struct Game { long PNo; //Player Number char PName[20]; long Points; }
Аnswer:
void SORTPOINTS(struct Game G[], int n) { int i, j; struct Game t; for(i=1;i<n;i++) { for(j=0;j<=n-i-1;j++) { if(G[j+1].Points>G[j].Points) { t = G[j]; G[j] = G[j+1]: G[j+1] = t; } } } }
Question 19:
Write a function SORTSCORE( ) in C++to sort an array of structure. Examinee in descending order of Score using bubble sort.
NOTE Assume the following definition of structure Examinee.
struct Examinee { long Roll No; char Name[20]; float Score; };
Аnswer:
void SORTSCORE(struct Examinee ex[],int n) { int i,j; struct Examinee t; for(i=1;i<n;i++) { for(j=0;j<=n-i-1;j++) { if(ex[j+1].Score>ex[j].Score) { t = ex[j]; ex[j] = ex[j+1]; ex[j+1] = t; } } } }
The post Important Questions for Class 12 Computer Science (C++) – Data Structure appeared first on Learn CBSE.