Assignment 3 - DATTV9
Assignment 3 - DATTV9
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;
}
}
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;
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++) {
//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;
// Tang
for (i = 0; i < n; i++) {
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
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;
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()
{ int x;
x=10;
fun(x);
cout << fun(x);
return 0;
}
Question 3
#include<iostream>
using namespace std;
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