0% found this document useful (0 votes)
10 views7 pages

Assignment Y-1

The document contains multiple C++ programming tasks, each with corresponding code examples. Tasks include computing the sum of square matrices, removing non-alphabet characters from a string, implementing power functions, and creating structures for students and employees with input and display functionalities. Each section provides a complete C++ program demonstrating the required functionality.

Uploaded by

kh4080cc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Assignment Y-1

The document contains multiple C++ programming tasks, each with corresponding code examples. Tasks include computing the sum of square matrices, removing non-alphabet characters from a string, implementing power functions, and creating structures for students and employees with input and display functionalities. Each section provides a complete C++ program demonstrating the required functionality.

Uploaded by

kh4080cc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q=1 Write a C++ program which computes sum of two square matrices using two dimensional array

in c++

#include <iostream>

using namespace std;

int main() {

int size;

cout << "Enter matrix size: ";

cin >> size;

int matrix1[size][size], matrix2[size][size];

cout << "Enter Matrix 1:\n";

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

cin >> matrix1[i][j];

cout << "Enter Matrix 2:\n";

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

cin >> matrix2[i][j];

matrix1[i][j] += matrix2[i][j];

cout << "Sum of Matrices:\n";

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

cout << matrix1[i][j] << " ";

cout << endl; }

return 0;

Q=2 Write a C++ program that takes a string from user and all remove characters from a string except alphabets.

#include <iostream>

using namespace std;


int main() {

string str;

cout << "Enter a string: ";

getline(cin, str);

for (int i = 0; i < str.length(); i++) {

if (!isalpha(str[i])) {

str.erase(i, 1);

i--;

cout << "String after removing non-alphabets: " << str << endl;

return 0;

Q =3 Write a C++ function power () which takes two integers x and y as parameters and returns the value of x y. The
prototype of your function must be int power (int, int). Write appropriate main function to test the power function.

#include <iostream>

using namespace std;

int power(int, int);

int main() {

int x, y;

cout << "Enter base (x): ";

cin >> x;

cout << "Enter exponent (y): ";

cin >> y;

int result = power(x, y);

cout << x << " raised to the power of " << y << " = " << result << endl;

return 0;

int power(int x, int y) {

int result = 1;
for (int i = 0; i < y; i++) {

result *= x;

return result;

Q=4 Write a C++ function power () which takes addresses of two integers x and y as parameters.The prototype of your
function must be void power (int* x, int* y). Write appropriate main function to test the power function

#include <iostream>

using namespace std;

void power(int* x, int* y) {

int res = 1;

for (int i = 0; i < *y; i++) {

res *= *x;

cout << *x << " raised to the power of " << *y << " = " << res << endl;

int main() {

int x, y;

cout << "Enter base: ";

cin >> x;

cout << "Enter exponent: ";

cin >> y;

power(&x, &y);

return 0;

Q=5 very long question

#include <iostream>

using namespace std;

const int MAXNAME = 10;

int main() {
int pos;

char * name;

int * one;

int * two;

int * three;

int result;

one = new int;

two = new int;

three = new int;

name = new char[MAXNAME];

cout << "Enter your last name with exactly 10 characters." << endl;

cout << "If your name has <10 characters, repeat last letter." << "Blanks at the end do not count." << endl;

for (pos = 0; pos < MAXNAME; pos++)

cin >> *(name + pos);

cout << "Hi ";

for (pos = 0; pos < MAXNAME; pos++)

cout << *(name + pos);

cout << endl;

cout << "Enter three integer numbers separated by blanks" << endl;

cin >> *one >> *two >> *three;

cout << "The three numbers are " << endl;

cout << *one << " " << *two << " " << *three << endl;

result = *one + *two + *three;

cout << "The sum of the three values is " << result << endl;

delete one;

delete two;

delete three;

delete[] name;

return 0;

Q=6 Create a struct teacher which has employee id, name, and designation pass it to function by value and assign value to it
now print it in main. Pass it to function by reference and assign value to it and print it in main

#include <iostream>

using namespace std;


const int MAXNAME = 10;

int main() {

char *name = new char[MAXNAME];

int *one = new int;

int *two = new int;

int *three = new int;

int result;

cout << "Enter your last name (10 chars): ";

for (int i = 0; i < MAXNAME; i++)

cin >> *(name + i);

cout << "Hi ";

for (int i = 0; i < MAXNAME; i++)

cout << *(name + i);

cout << endl;

cout << "Enter three numbers: ";

cin >> *one >> *two >> *three;

cout << "Numbers: " << *one << " " << *two << " " << *three << endl;

result = *one + *two + *three;

cout << "Sum: " << result << endl;

delete[] name;

delete one;

delete two;

delete three;

return 0;

Q=7 Create a struct student which has student id, name and cgpa as members. Create a static array of size 05 of type struct
student. Write a function Input_Record for taking record of 05 students. Write a function display to print contents of array on
output screen. C++

#include <iostream>

#include <string>

using namespace std;


struct Student {

int id;

string name;

float cgpa;

};

void Input_Record(Student students[]) {

for (int i = 0; i < 5; i++) {

cout << "Enter Student " << i + 1 << " details:" << endl;

cout << "ID: ";

cin >> students[i].id;

cout << "Name: ";

cin.ignore();

getline(cin, students[i].name);

cout << "CGPA: ";

cin >> students[i].cgpa;

cout << endl;

void Display(Student students[]) {

cout << "Student Records:" << endl;

for (int i = 0; i < 5; i++) {

cout << "ID: " << students[i].id << endl;

cout << "Name: " << students[i].name << endl;

cout << "CGPA: " << students[i].cgpa << endl;

cout << endl;

int main() {

Student students[5];

Input_Record(students);

Display(students);

return 0;

Q=9 Create a struct employee which has employee_id, name and name, age as members. Create a dynamic array of size 05
of type struct employee. Write a function Input_Record for taking record of 05 employees. Write a function display to print
contents of array on output screen.

#include <iostream>

#include <string>

using namespace std;


struct Employee {

int id;

string name;

int age;

};

void Input_Record(Employee* e, int size) {

for (int i = 0; i < size; i++) {

cout << "Enter Employee " << i + 1 << " details:" << endl;

cout << "ID: ";

cin >> e[i].id;

cout << "Name: ";

cin.ignore();

getline(cin, e[i].name);

cout << "Age: ";

cin >> e[i].age;

cout << endl;

void Display(Employee* e, int size) {

cout << "Employee Records:" << endl;

for (int i = 0; i < size; i++) {

cout << "ID: " << e[i].id << endl;

cout << "Name: " << e[i].name << endl;

cout << "Age: " << e[i].age << endl;

cout << endl;

int main() {

const int size = 5;

Employee* employees = new Employee[size];

Input_Record(employees, size);

Display(employees, size);

delete[] employees;

return 0;}

You might also like