0% found this document useful (0 votes)
84 views

C++ Assignment

The document contains 45 questions asking to write functions in C++ to perform various operations on arrays and matrices. The questions cover basic array operations like printing, reversing, summing; more advanced operations like sorting, searching, merging arrays; and matrix operations like addition, subtraction, multiplication, and finding transpose. The goal is to practice writing reusable functions to work with arrays and matrices in C++.

Uploaded by

Syed Arslan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

C++ Assignment

The document contains 45 questions asking to write functions in C++ to perform various operations on arrays and matrices. The questions cover basic array operations like printing, reversing, summing; more advanced operations like sorting, searching, merging arrays; and matrix operations like addition, subtraction, multiplication, and finding transpose. The goal is to practice writing reusable functions to work with arrays and matrices in C++.

Uploaded by

Syed Arslan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Assignments Programming

Assignment 7 - Functions
Questions:
1. Write a function to get an array of integers as input parameters and print it.
2. Write a function to display an array in reverse order.
3. Write a function to find the sum of all elements of the array.
4. Write a function to copy the elements of one array into another array.
5. Write a function to count a total number of duplicate elements in an array.
6. Write a function to print all unique elements in an array.
7. Write a function to merge two arrays of same size sorted in decending order.
8. Write a function to count the frequency of each element of an array
9. Write a function to find the maximum and minimum element in an array.
10. Write a function to separate odd and even integers in separate arrays.
11. Write a function to sort elements of array in ascending order.
12. Write a function to sort elements of the array in descending order.
13. Write a function to insert New value in the array (sorted list )
14. Write a function to insert New value in the array (unsorted list ).
15. Write a function to delete an element at desired position from an array.
16. Write a function to find the second largest element in an array.
17. Write a function to find the second smallest element in an array.
18. Write a function to get input from user for a 2D array of size 3x3 and print the matrix.
19. Write a function to add two Matrices of same size.
20. Write a function to subtract two Matrices
21. Write a function to multiply two square Matrices.
22. Write a function to find transpose of a given matrix.
23. Write a function to find sum of right diagonals of a matrix
24. Write a function to find the sum of left diagonals of a matrix.
25. Write a function to find sum of rows an columns of a Matrix.
26. Write a function to print or display the lower triangular of a given matrix.
27. Write a function to print or display upper triangular matrix
28. Write a function to calculate determinant of a 3 x 3 matrix.
29. Write a function to accept two matrices and check whether they are equal.
30. Write a function to check whether a given matrix is an identity matrix.
31. Write a function to find a pair with given sum in the array
32. Write a function to find the majority element of an array.
33. Write a function to find the number occurring odd number of times in an array.
34. Write a function to find the largest sum of contiguous subarray of an array.
35. Write a function to find the missing number from a given array. There are no duplicates in list.
36. Write a function to merge one sorted array into another sorted array.
37. Write a function to find the two repeating elements in a given array
38. Write a function to find two elements whose sum is closest to zero
39. Write a function to find the smallest positive number missing from an unsorted array
40. Write a function to find a subarray with given sum from the given array.
41. Write a function to find a given number using liner search
42. Write a function to find a given number using Binary search
43. Write a function to move all zeroes to the end of a given array
44. Write a function to find the row with maximum number of 1s in a 2D Array
45. Write a function to print all unique elements of an unsorted array

Best of luck :-)

——————————————————————————————————————————————
—————————

For Me : https://www.w3resource.com/c-programming-exercises/array/index.php
Assignment 6

Links To Study:
For understanding how Arrays work, please check this link:
1. https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm
2. https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm
Questions:
1. Write a program in C to store elements in an array and print it.
2. Write a program in C to read n number of values in an array and display it in reverse order.
3. Write a program in C to find the sum of all elements of the array.
4. Write a program in C to copy the elements of one array into another array.
5. Write a program in C to count a total number of duplicate elements in an array.
6. Write a program in C to print all unique elements in an array.
7. Write a program in C to merge two arrays of same size sorted in decending order.
8. Write a program in C to count the frequency of each element of an array
9. Write a program in C to find the maximum and minimum element in an array.
10. Write a program in C to separate odd and even integers in separate arrays.
11. Write a program in C to sort elements of array in ascending order.
12. Write a program in C to sort elements of the array in descending order.
13. Write a program in C to insert New value in the array (sorted list )
14. Write a program in C to insert New value in the array (unsorted list ).
15. Write a program in C to delete an element at desired position from an array.
16. Write a program in C to find the second largest element in an array.
17. Write a program in C to find the second smallest element in an array.
18. Write a program in C for a 2D array of size 3x3 and print the matrix.
19. Write a program in C for addition of two Matrices of same size.
20. Write a program in C for subtraction of two Matrices
21. Write a program in C for multiplication of two square Matrices.
22. Write a program in C to find transpose of a given matrix.
23. Write a program in C to find sum of right diagonals of a matrix
24. Write a program in C to find the sum of left diagonals of a matrix.
25. Write a program in C to find sum of rows an columns of a Matrix.
26. Write a program in C to print or display the lower triangular of a given matrix.
27. Write a program in C to print or display upper triangular matrix
28. Write a program in C to calculate determinant of a 3 x 3 matrix.
29. Write a program in C to accept two matrices and check whether they are equal.
30. Write a program in C to check whether a given matrix is an identity matrix.
31. Write a program in C to find a pair with given sum in the array
32. Write a program in C to find the majority element of an array.
33. Write a program in C to find the number occurring odd number of times in an array.
34. Write a program in C to find the largest sum of contiguous subarray of an array.
35. Write a program in C to find the missing number from a given array. There are no duplicates in list.
36. Write a program in C to merge one sorted array into another sorted array.
37. Write a program in C to find the two repeating elements in a given array
38. Write a program in C to find two elements whose sum is closest to zero
39. Write a program in C to find the smallest positive number missing from an unsorted array
40. Write a program in C to find a subarray with given sum from the given array.
41. Write a program in C to find a given number using liner search
42. Write a program in C to find a given number using Binary search
43. Write a program in C to move all zeroes to the end of a given array
44. Write a program in C to find the row with maximum number of 1s in a 2D Array
45. Write a program in C to print all unique elements of an unsorted array

Best of luck :-)

——————————————————————————————————————————————
—————————
Assignment 5

Links To Study:

For understanding how for loops work check this link: https://beginnersbook.com/2017/08/cpp-for-
loop/
For understanding how while loops work check this link: https://beginnersbook.com/2017/08/cpp-
while-loop/
For understanding how do-while loops work check this link: https://beginnersbook.com/2017/08/cpp-
do-while-loop/

Questions:

1. Write a program in C++ to find the first 10 natural numbers? E.g: 1, 2, 3, … , 10


2. Write a program in C++ to find the sum of first 10 natural numbers?
3. Write a program in C++ to display n terms of natural number and their sum?
4. Write a program in C++ to find the perfect numbers between 1 and 500. (The perfect numbers
between 1 to 500 are: 6, 28, 496)
5. Write a program in C++ to check whether a number is prime or not?
6. Write a program in C++ to find prime number within a range?
7. Write a program in C++ to find the factorial of a number
8. Write a program in C++ to find the last prime number occur before the entered number?
9. Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers?
10. Write a program in C++ to find the sum of digits of a given number?
11. Write a program in C++ to find the sum of the series? Series = 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n
12. Write a program in C++ to calculate the sum of the series? Series = (1*1) + (2*2) + (3*3) + (4*4) +
(5*5) + ... + (n*n)
13. Write a program in C++ to calculate the series? Series = (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n)
14. Write a program in C++ to find the sum of series? Series = 1 - X^2/2! + X^4/4!-.... upto nth term
15. Write a program in C++ to asked user to input positive integers to process count, maximum,
minimum, and average or terminate the process with -1
16. Write a program in C++ to list non-prime numbers from 1 to an upperbound?
17. Write a program in C++ to print a square pattern with # character after taking input from user about
number of rows?
18. Write a program in C++ to display the multiplication table of a number input from user?
19. Write a program in C++ to display the first n terms of Fibonacci series?
20. Write a program in C++ to find the number and sum of all integer between 100 and 200 which are
divisible by 9?
21. Write a program in C++ to display the number in reverse order?
22. Write a program in C++ to find the length of a string without using the library function?
23. Write a program in C++ to input a saved password from user and then show “”Enter your password
to login:” then check while the password is not equal to saved password?
24. Create a menu base calculator program in C++ where u will take inputs from user for the option they
want and then take inputs accordingly.
1. Sample Calculator:
1. Enter 1 to add two numbers
2. Enter 2 to subtract two numbers
3. Enter 3 to multiply two numbers
4. Enter 4 to divide two numbers
5. Enter -1 to exit the program.
2. Let say user enters 1 then ask user to input 2 numbers and show its output and take user back to
menu for input of new action by user.
25. Write a program in C++ to display the following pattern shapes? (1 2 3 are bullets so ignore them just
draw the * shape using loops)

Shape 1:

*
**
*
**
***
Shape 2:

*
*
***
***
***

Shape 3:

1
12
123
1234
12345

Shape 4: (Input row size from user)


1
22
333
4444
55555

Shape 5: (Input row size from user)

1
23
456
7 8 9 10
Shape 6: (Half Diamond - Input row size from user)

*
***
*****

Shape 7: (Full Diamond - Input row size from user)

*
***
*****
***
*

Challenge Questions:

1. Write a program in C++ to display Pascal's triangle like pyramid.


Sample Output: Input number of rows: 5

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Important:

Don’t cheat from google. Try yourself and ask me incase you’re not understanding anything.
Assignment 4

For understanding how if-else works, please check this link: https://www.programiz.com/cpp-
programming/if-else
For switch statement please check this link: https://www.programiz.com/cpp-programming/switch-case

Questions:

1. Take values of length and breadth of a rectangle from user and check if it is square or not?
2. Take two int values from user and print greatest among them?
3. A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
4. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
5. Take input of age of 3 people by user and determine oldest and youngest among them?
6. Write a program to print absolute value of a number entered by user?
7. A student will not be allowed to sit in exam if his/her attendance is less than 75%
Take following input from user:
Number of classes held
Number of classes attended.
And print percentage of class attended, Is student allowed to sit in exam or not?
8. Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has
medical cause or not ( 'Y' or 'N' ) and print accordingly.
9. Write a program to check whether an entered character is lowercase ( a to z ) or uppercase ( A to Z )?
10. Take number input from user and check if that number is fully divisible by 10 or not?
11. Take input from user a 5 digit number in one variable and print its inverse?
12. Take input from user a string and print if its a palindrome or not?
13. Write a program to check if a given year by user is leap year or not? If year is exactly divisible by 4
and not divisible by 100, then it is leap year. Or if year is exactly divisible by 400 then it is leap year.
14. Write a C program to print day of Week name using switch case?
15. Write a C program to print total number of days in a month using switch case?
16. Write a C program to check whether an alphabet is vowel or consonant using switch case.
17. Write a C program to find maximum between two numbers using switch case.
18. Write a C program to check whether a number is even or odd using switch case.
19. Write a C program to check whether a number is positive, negative or zero using switch case.
20. Write a C program to create Simple Calculator using switch case.

Challenge Questions:

1. What is the outputted of following code? (There could be a compile time error as well - Don’t copy
paste the code in IDE. I trust you.)

A.
Int x=5;
if(x=6){
print “I can be a good programmer”
}
else {
print “I am a good programmer”
}

B.
Int x=5;
if(x){
print “Hello World”
}

C.
Int x=5;
if(x)
print “Hello World”
print “Hello World Again”
else
print “Hello World with else case”

D.
if(1)
print “Hey You!”
else
print “Not You!”

2. What is difference between if/else and Ternary operator?

Important:

Don’t cheat from google. Try yourself and ask me incase you’re not understanding anything.

Best of luck :-)

——————————————————————————————————————————————
—————————
Assignment 3

Questions:

1. Write a program in C++ to print the sum of two numbers using variables?
2. Write a program in C++ to swap two numbers?
3. Write a program in C++ to calculate the volume of a sphere by taking input of radius from user?
Volume: 4πr3
4. Write a program in C++ to calculate the volume of a cube? Volume: side3
5. Write a program in C++ to find the Area and Perimeter of a Rectangle? Area: Length * Width —
Perimeter: 2 * (Length + Width)
6. Write a program in C++ to find the area and circumference of a circle? Area: πr2 Circumference: 2πr
7. Write a program in C++ to convert temperature in Celsius to Fahrenheit? celsius = ((Fahrenheit * 5.0)-
(5.0 * 32))/9
8. Write a program in C++ to find the third angle of a triangle? ang3=180-(ang1+ang2)
9. Write a program in C++ that converts kilometres per hour to miles per hour? milesPerHour =
(KilometersPerHour * 0.6213712)
10. Write a program in C++ to compute quotient and remainder? quotient=dividend / divisor and
remainder=dividend % divisor
11. Write a program in C++ to compute the total and average of four numbers?
12. What is typecasting? Give at least 2 examples using code?
13. Write a program in C++ that takes a number as input and prints its multiplication table up to 10?
14. Input a 5 digit number from user and print its reverse? Please don’t use arrays as we’ve not studied
them yet. Do it with operators?

Challenge Questions:

1. Give two solutions to swap the values of two variables without using third variable?

A=5
B = 10
Case 2

A=a

Case 1

A = a+b - 15
B = a-b - 5
A = a-b = 10

Important Instructions:

1. I want you to implement the programs on computer now.

PROHIBITED: YOU WILL NOT GOOGLE THE CODE/ANSWERS.

Best of luck :-)


Assignment 2

Assignment 2 (Need to memorise it - You will get use to of it by using some of them most of the times
but memorise all - I recommend it fully to memorise all)

Follow this link for help: https://data-flair.training/blogs/operators-in-c-and-cpp/

Questions:
1. What are data types?
2. What are common/important datatypes (Please mention names here)
3. what is difference between variable and constant?
4. What is difference between declaration & initialisation?
5. What happens when you declare a variable? (Understand the memory management - difficult for u at
this stage but give it a try otherwise I’ll explain.)
6. What is sizeOf() use for? (Important: understand here what are sizes of data types in memory)
7. What is an operator?
8. What is an operand?
9. How many types of operators?

Additional Questions (Bonus :p) - These will differentiate you from others and I guarantee, it will put you
in a A CLASS Coder circle.
1. What is stack & heap memory?
2. Which space in computer is called volatile and why?
3. Your variable declarations & Initialisations are stored where? (Heap or stack or anywhere else)
4. When I declare an integer let say 4 in my c++ program, What actually is happening here (Deep dive as
max as you can!!!) - I loved this understanding. Special respect for those who has this level of
knowledge.
5. Where are operators and operands saved? (Kind of same question as Point # 3)

Tasks:

- Go through each and every operator detail


- Understand how it works
- Should know how many operands are required for all operators
- Write a program where you’ll use all operators and comment how they are working and on how many
operands
- For Point # 4 create a C++ program by yourself - I trust you and hope you won’t copy from google. Do
check if not understanding or ask me.

Special Tasks:

- I recommend if you want to become a really good developer/software-Engineer, keep this in mind,
C/C++ are one of the most difficult high level languages in this world just because of memory
management and pointers. You get the full access of memory in C/C++. So your concept of memory
management for variables in a C/C++ program should be damn clear - I repeat Damn CLEAR!!!

Best of luck :-)

——————————————————————————————————————————————
——————————————

Assignment 1

Please find the following as our next steps/plan. Let me know incase you’ve any concerns or queries in
it.

Short summary of Plan:

I’ll share some questions/code most of the times with you and you’ll do your research on it to solve
them. After that I’ll ask you three questions and If you will give me right answer, we’ll go to the next
topic.

For a quick start or a warmup, I’ve added some information below. Please go through it and give
yourself a kick to get ready for programming. We’re literally gonna control the machines :p haha thats a
fancy line but It is true as we will be coding to make machine work for us what ever we want. At the end
we can have a money making machine (Now thats a joke :p).
So let’s start!!!

Step 1: Warm Up Session

For a warm up, go through the slides : https://www.slideshare.net/rehan410/programming-


fundamentals-lecture-2

Just simply go through, no need to memorise everything for a decade. Just understand what it is, how
and why?

Ask me any question as many as times you want. (I’ll prefer calls for these btw as it will save my time for
typing :p but can answer you via email as well.)

We are going to start learning programming in c++ language.

Some Pre-Requisites I want you to understand includes following,

1. What is computer? (I know you already know :p)


2. What is a program?
3. What is programming?
4. What is pseudocode, flow chart & algorithm?
5. What is compiler?
6. What are pre-processor directives?
7. What is Assembler?
8. What is difference between HLL and LLL? (High level language & Low level language)
9. What is linker?
10. What is loader?
11. How does a computer run a program?
12. What are steps to run a program? (Sounds same like Q11 & it is just writing it twice so you can see
I’m emphasising on it.)
13. What is IDE? (I’ve used DevC/DevC++, CodeBlocks & Visual Studio. I’ll recommend you to save time
and install DevC++ for now. In future we’ll jump to Visual Studio In Sha Allah)

That’s it. After we’re done with these pre-requisites, we are good to go for a 3 questions steps :-) May
favourite part. You’ll like it too as its too much informative and has great impact. (Sounds like apnay mo
miyan meetho but you’ll see :p)

Please follow the link: https://www.design-reuse.com/articles/37754/building-process-for-the-c-c-


program-on-complex-socs.html to understand how a C/C++ program runs step by step.

Step 2: If you’re done with step 1, write a hello world program in DevC++ using C++ Language.

You can get help from anywhere - not an issue. but need to explain me how you solved it and why.

Simple isn’t it?

Best of luck :-)

You might also like