0% found this document useful (0 votes)
184 views13 pages

Assignment 3 - DATTV9

The document provides code examples and explanations for 6 assignments involving pointers and references in C++. The assignments cover topics like adding two matrices using pointers, reversing a string using pointers, sorting and ordering arrays using pointers, checking if a number is a palindrome, and converting decimal to hexadecimal. It also provides sample inputs and outputs for each problem and predicts the output for some example programs involving pointers and references.

Uploaded by

đạt tiêu
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)
184 views13 pages

Assignment 3 - DATTV9

The document provides code examples and explanations for 6 assignments involving pointers and references in C++. The assignments cover topics like adding two matrices using pointers, reversing a string using pointers, sorting and ordering arrays using pointers, checking if a number is a palindrome, and converting decimal to hexadecimal. It also provides sample inputs and outputs for each problem and predicts the output for some example programs involving pointers and references.

Uploaded by

đạt tiêu
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/ 13

CODE: CPP.Assignment03.

Opt1 FRESHER
Assignment topic : Pointers and References
ACADEMY
Assignment duration : 240 minutes

Bài 1. Write a C++ program to add two matrix using pointers. C program to input two matrix from user
and find sum of both matrices using pointers.
Sample:
Input matrix1:
123
456
789
Input matrix2:
987
654
321
Sum of both matrices:
10 10 10
10 10 10
10 10 10
#include<iostream>
using namespace std;
void nhap(int **a,int n,int m){
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++){
cin >> a[i][j];
}
}
void xuat(int **a,int n,int m){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cout << a[i][j] << " ";
}
cout << endl;
}
}

int** sum(int **a,int **b,int n,int m){


int **c = new int*[n];
for(int i = 0;i < m;i++)
c[i] = new int[m];

for(int i = 0;i<n;i++)
for(int j = 0;j < m;j++)
c[i][j] = a[i][j] + b[i][j];

return c;
}

int main(){
int n,m;
cout <<"Nhap so dong,cot : ";
cin >> n >> m;

int **a = new int*[n];


for(int i = 0;i<m;i++)
a[i] = new int[m];

int **b = new int*[n];


for(int i = 0;i<m;i++)
b[i] = new int[m];
cout << "Nhap ma tran a:" <<endl;
nhap(a,n,m);
cout << "Nhap ma tran b:" <<endl;
nhap(b,n,m);

cout << "Sum of both matrices:\n";


xuat(sum(a,b,n,m),n,m);

for(int i = 0;i<m;i++)
delete []a;
delete []a;

for(int i = 0;i<m;i++)
delete []b;
delete []b;

return 0;
}
Cách 2:
#include<iostream>
#include <iomanip>
using namespace std;
int main() {
int matran1[10][10], matran2[10][10], matran3[10][10], row1, col1, row2, col2;
cout << "Nhap so hang cua ma tran 1:";
cin >> row1;
cout << "Nhap so cot cua ma tran 1:"; cin >> col1;
cout << "Nhap so hang cua ma tran 2:"; cin >> row2;
cout << "Nhap so cot cua ma tran 2:"; cin >> col2;

//In ma tran 1
cout << "Input Matrix\n";
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {

cin >> matran1[i][j];


}
}
//In ma tran 2
cout << "Input Matrix\n";
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {

cin >> matran2[i][j];


}
}

//Tong 2 ma tran
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
matran3[i][j] = matran1[i][j] + matran2[i][j];
}
}
//In ma tran 3
cout << "Sum of both matrix:\n";
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
printf("%d\t", matran3[i][j]);
}
printf("\n");
}
return 0;
}
Bài 2. Write a C++ program to find reverse of a string using pointers.
Sample:
Input = HELLO
Output = OLLEH

#include <string.h>
#include <iostream>
using namespace std;
int main(){
char *str="HELLO";
cout<<"Input::"<<str;
cout<<endl<<"Output::";
for(int i=(strlen(str)-1);i>=0;i--){
cout<<str[i];
}
return 0;
}
Bài 3. Write a C++ program to sort array using pointers.
Sample:
Input:
Input array elements: 10 -1 0 4 2 100 15 20 24 -5
Output:

Array in ascending order: -5, -1, 0, 2, 4, 10, 15, 20, 24, 100,
Array in descending order: 100, 24, 20, 15, 10, 4, 2, 0, -1, -5,
#include <iostream>
using namespace std;

//void nhap(int ){}


void sort(int n, int* ptr)
{
int i, j, t;

// Tang
for (i = 0; i < n; i++) {

for (j = i + 1; j < n; j++) {

if (*(ptr + j) < *(ptr + i)) {

t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}

// print the numbers


cout<<"Array in ascending order:" ;
for (i = 0; i < n; i++)
printf("%d ", *(ptr + i));
printf("\n");
}

void sortdown(int n, int* ptr)


{
int i, j, t;

// Sort the numbers using pointers


for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {

if (*(ptr + j) > *(ptr + i)) {

t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}

// print the numbers


cout<<"Array in descending order:" ;
for (i = 0; i < n; i++)
printf("%d ", *(ptr + i));
}
// Driver code
int main()
{
int n = 5;
int arr[] = { 0, 23, 14, 12, 9 };

sort(n, arr);
sortdown(n,arr);

return 0;
}
Bài 4 (bonus). Write a C++ program to check whether a number is palindrome or not.
Sample:
Input
Input any number: 121
Output
121 is palindrome

#include <iostream>
using namespace std;

int main()
{
int n, num, digit, rev = 0;

cout << "Enter a positive number: ";


cin >> num;

n = num;

do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);

if (n == rev)
cout <<n<< " is a palindrome.";
else
cout <<n<< " is not a palindrome.";

return 0;
}
Bài 5 (bonus). Write a C++ program to convert Decimal to Hexadecimal number system
Sample:
Input
Input decimal number: 26
Output
Hexadecimal number: 1A

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int dec_num, r;
string hexdec_num="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
cout << " Input a decimal number: ";
cin>> dec_num;

while(dec_num>0)
{
r = dec_num % 16;
hexdec_num = hex[r] + hexdec_num;
dec_num = dec_num/16;
}
cout<<" The hexadecimal number is : "<<hexdec_num<<"\n";
}
Bài 6. Predict the output of following programs. If there are compilation errors, then fix them:
Question 1
#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
 30
Question 2
#include<iostream>
using namespace std;

int fun(int &x)


{
return x;
}
int main()
{
cout << fun(10);
return 0;
}
 Error
#include<iostream>
using namespace std;

int fun(int& x)
{
return x;
}
int main()
{ int x;
x=10;
fun(x);
cout << fun(x);
return 0;
}

Question 3
#include<iostream>
using namespace std;

void swap(char * &str1, char * &str2)


{
char *temp = str1;
str1 = str2;
str2 = temp;
}

int main()
{
char *str1 = "GEEKS";
char *str2 = "FOR GEEKS";
swap(str1, str2);
cout<<"str1 is "<<str1<<endl;
cout<<"str2 is "<<str2<<endl;
return 0;
}
 Str1 is “FOR GEEKS”
 Str2 is “GEEKS”
Question 4
#include<iostream>
using namespace std;

int main()
{
int x = 10;
int* ptr = &x;
int &*ptr1 = ptr;
}
 Error
#include<iostream>
using namespace std;

int main()
{
int x = 10;
int *ptr = &x;
int *ptr1 ;
ptr1 = ptr;
cout << *ptr;
}
Question 5
#include<iostream>
using namespace std;

int main()
{
int *ptr = NULL;
int &ref = *ptr;
cout << ref;
}
 NULL

Question 6
#include<iostream>
using namespace std;

int &fun()
{
int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
 10

You might also like