Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
81 views11 pages

C++ Homework & Programming Tasks

The document contains 6 programming questions that involve arrays, strings, matrices, credit card validation, and dynamic memory allocation. The questions require writing functions to calculate element frequencies in an array, validate a credit card number, store student marks in a 2D matrix, find even/odd max/min of numbers entered until a negative number using a dynamically allocated array, sort and print the union of two dynamically allocated arrays, and divide a string into equal parts based on user input.
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)
81 views11 pages

C++ Homework & Programming Tasks

The document contains 6 programming questions that involve arrays, strings, matrices, credit card validation, and dynamic memory allocation. The questions require writing functions to calculate element frequencies in an array, validate a credit card number, store student marks in a 2D matrix, find even/odd max/min of numbers entered until a negative number using a dynamically allocated array, sort and print the union of two dynamically allocated arrays, and divide a string into equal parts based on user input.
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/ 11

Homework # 12:

Theoretical questions:-

Q1) Which of the following is FALSE about references in C++?


a) References cannot refer to a constant value.
b) References cannot be NULL.
c) A reference must be initialized when declared.
d) Once a reference is created, it cannot be later made to reference another
object; it cannot be reset.

Q2)What is the output of the following code?


Write -1 if the code has a compile-time error and -2 if the code has a run-time
error.
Q3)What is the output of the following code?

a) 90
b) 110
c) 100
d) 60

Q4) What is the output of the following code?


a) Output is 4 because in fun(), the pointer is modified to point to the variable “a”.
b) The program may show a run time error because the pointer “p” points to the
variable “a” whose scope has ended.
c) Output is 5 because fun() only modifies its local copy of pointer “p”. Pointer “p”
in main still points to the variable b.
d) The code does not compile.

Q5) What is the output of the following code?


Programming Questions:-

Ques 1: Write a program to calculate the number of times each element is


repeated in the array. You can assume that the values in the array are between -
100 and 100, both inclusive. For example, the following output should be
displayed for the array A = {7, 8, -1, 4, 7, 10, -1, 5, 8, 10, 4, 7, -1}.

The program should be general enough to display the counts of each integer for
an array of any length

Ques 2. In this task, you have to solve a real-world problem, where your function
will take any real credit card number as a string, and you have to tell whether the
entered credit card number is valid or not. Most credit cards and many
government identification numbers use the algorithm as a simple method of
distinguishing valid numbers from mistyped or otherwise incorrect numbers. It
was designed to protect against accidental errors, not malicious attacks. Let’s
suppose the valid credit card number entered is 79927398713.

1. Starting from the rightmost digit, double the value of every second digit,

2. If doubling of a number results in a two-digit number, i.e., greater than


9(e.g., 6 × 2 = 12), then add the digits of the product (e.g., 12: 1 + 2 = 3,
15: 1 + 5 = 6), to get a single digit number.

3. Now, take the sum of all the digits.

If the total modulus 10 is equal to 0 (if the total ends in zero), then the
number is valid according; else, it is not valid.
Since the sum is 70, which is a multiple of 10, the account number is
valid.

Enter Credit Card Number: “79927398713“


Entered Card Number is Valid
Enter Credit Card Number: “32323”
Entered Credit Card Number is Invalid

In the end, double-check the program by entering your credit card number.
Have Fun!!

Question 3. In this task, you have to make a matrix for a class where you will
store marks of Mathematics, Science, and English for every student(using a 2D
array). In a 2D array, you’ll use the number of rows as the number of students
and the number of columns to represent the total subjects(which is 3 in this
case). First, input all the students' names according to the number of students in
the class in the 1-D array.
Next, initialize the matrix with the number of rows and columns as explained
above.
Next, input the scores for each student in each of the respective subject columns.

Print the resultant matrix formed in the correct matrix form.

Write a function that takes the matrix as input and returns the student name with
the highest total marks.

Write another function that takes the student's name and prints the marks of the
student in each of the three subjects, obtained marks, and overall
percentage(considering total marks: 100)
Answer:-

Question 4. To familiarise you guys with the syntax of dynamic memory


allocation and the use of pointers, Write a simple program that takes a sequence
of positive numbers until the user enters a negative number; you have to store all
the numbers in an array. You have to print the even maximum and odd minimum
of the numbers in the array. (Restriction: You can only create a dynamic array and only
access the array through pointers, not with indexes.)
Hint: use *(arr+0) instead of arr[0].

Make sure that if the entered numbers have no odd or even digit, print the
following: “NO ODD/EVEN INTEGERS IN THE ARRAY”
Answer:-

Question 5: In this task, you’ve to deal with two arrays that could be unordered
and of different sizes. You have to take inputs of numbers from the user until the
user enters a negative number for both of the arrays. Because the size of the
array depends on the input values, which can vary in this problem, so normal
arrays cannot be used here because we have to declare them with a fixed size. If
we provide the array with the excess size, then definitely, it results in the wastage
of the memory of our system.
You have to use dynamic arrays initialized as follows:
int *arr1=new int(1);
int* arr2=new int(1);

Your job in this task is to sort the two arrays and then print the Union of two
arrays.

A=[1,2,3,4,5,5]
B=[4,5,9,10]
Result:-
C=A U B=[1,2,3,4,5,9,10]
ANSWER:-

Question 6: In this task, you have to input a string. Then, you will take input
from the user a number that represents the number of equal parts in which you
want to divide the string. If division into equal parts is not possible, print “NOT
POSSIBLE”. Then you have to enter the part number of the divided string which
you want to display. In the end, you also have to display the combined ignored
string that the user doesn’t want to display
Answer:-

You might also like