0% found this document useful (0 votes)
21 views15 pages

NMĐT Lab 4

Uploaded by

minhngoctu12
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)
21 views15 pages

NMĐT Lab 4

Uploaded by

minhngoctu12
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/ 15

LAB 4

Thái Phụng Tuấn - 2453365


Exercise 3:
#include <iostream>

using namespace std;

void solution(int);

int main(){

int length;

while (true){

cout << "Type in the length of the array:";

cin >> length;

if (length <= 0)

cout << "The length must be a positive number. Try again."<< endl;

else break;

solution (length);

return 0;

void solution (int a){

float RangeOfNum[a];

float test;

for (int i=1; i<=a; i++){

cout << "Type in the "<<i<<" element of the array:";

cin >> RangeOfNum[i-1];

test = RangeOfNum[0];

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

if (test < RangeOfNum[i]){

test = RangeOfNum[i];

cout << "The largest element in the array is: "<< test;

Exercise 4:

#include <iostream>

using namespace std;

void average(int);

int main()

int length;

while (true){

cout << "Type in the length of the array:";

cin >> length;

if (length <= 0)

cout << "The length must be a positive number. Try again."<< endl;

else break;

average(length);

return 0;

void average(int a){

float RangeOfNum[a];

2
float result;

float sum =0;

for (int i=1; i<=a; i++){

cout << "Type in the "<<i<<" element of the array:";

cin >> RangeOfNum[i-1];

sum += RangeOfNum[i-1];

result = sum/a;

cout << "The average value of the array is: "<< result;

Exercise 5:
#include <iostream>

#include <math.h>

using namespace std;

double solution(double, int);

int main()

{ double base;

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

cin >> base;

int exponent;

cout << "Enter the exponent (n):";

cin >> exponent;

// Xuất ra kết quả

cout << base << " to the power of " <<exponent<<" is " << solution(base,exponent);

3
double solution(double a, int b){

double result=1.0;

int abs_b = abs(b);

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

result *= a;

// Nếu số mũ là âm thì kết quả là nghịch đảo

if (b<0)

result = 1/result;

return result;

Exercise 6:
#include <iostream>

using namespace std;

void solution(int);

int main(){

int length;

while (true){

cout << "Type in the length of the array:";

cin >> length;

if (length <= 0)

cout << "The length must be a positive number. Try again."<< endl;

else break;

solution (length);

return 0;

4
void solution (int a){

float RangeOfNum[a];

float test;

for (int i=1; i<=a; i++){

cout << "Type in the "<<i<<" element of the array:";

cin >> RangeOfNum[i-1];

int PostCount = 0; int NeCount = 0;

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

if (RangeOfNum[i]=0){

continue;

else if (RangeOfNum[i]>0){

PostCount++;

else NeCount++;

cout << "The number of positive value: "<<PostCount << endl;

cout << "The number of negative value: "<< NeCount;

Exercise 7:
#include <iostream>

using namespace std;

void solution(int);

int main(){

int length;

while (true){

5
cout << "Type in the order of the array:";

cin >> length;

if (length <= 0)

cout << "The order must be a positive number. Try again."<< endl;

else break;

solution (length);

return 0;

void solution (int n){

int matrix[100][100], transposed[100][100];

cout << "Enter the elements of the matrix row by row:" << endl;

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

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

cin >> matrix[i][j];

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

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

transposed[j][i] = matrix[i][j];

cout << "\nTransposed Matrix:" << endl;

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

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

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

cout << endl;

6
}

Exercise 8:
#include <iostream>

#include <math.h>

using namespace std;

int main(){

double ApproximateValue=0.0;

double PreviousValue = 1.0;

int n=0;

const double tolerance = 1.0E-6;

while (abs(ApproximateValue - PreviousValue) >= tolerance){

PreviousValue = ApproximateValue;

double term =(n%2==0? 1.0 : -1.0)/(2*n+1);

ApproximateValue += term;

n++;

double pi= ApproximateValue *4;

cout << "The approximate value of pi is: "<< pi;

return 0;

Exercise 9:
#include <iostream>

#include <math.h>

#define PI 3.141592653589793

using namespace std;

7
int main() {

int x=5;

//Start to calculate

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

//Convert to radians

double RadValue = x*PI/180;

//Tabulate

double Cos = cos(RadValue);

double Sin = sin(RadValue);

double Tan = tan(RadValue);

cout << "The value of cos(" <<x<< ") is: "<<Cos<< endl

<< "The value of sin(" <<x<< ") is: "<< Sin<< endl

<< "The value of tan(" <<x<< ") is: "<< Tan<< endl<<endl;

x+=5;

return 0;

Exercise 10:
#include <iostream>

#include <msth.h>

using namespace std;

double factorial(int n) {

if (n == 0 || n == 1) {

return 1;

return n * factorial(n - 1);

8
int main() {

double approximateE = 1.0;

double previousApproximation = 0.0;

double tolerance = 1.0E-6;

int n = 1;

while (abs(approximateE - previousApproximation) >= tolerance) {

previousApproximation = approximateE;

approximateE += 1.0 / factorial(n);

n++; /

cout << "The approximate value of e is: " << approximateE << endl;

return 0;

Exercise 11:
#include <iostream>

using namespace std;

long long fibonacci(int n) {

if (n == 0) return 0;

if (n == 1) return 1;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int n;

cout << "Enter the position (n) of the Fibonacci sequence: ";

cin >> n;

9
if (n < 0) {

cout << "Invalid input! n must be a non-negative integer." << endl;

return 1;

cout << "The Fibonacci number at position " << n << " is: " << fibonacci(n) << endl;

return 0;

Exercise 12:
#include <iostream>

#include <math.h>

//#define PI 3.141592653589793

using namespace std;

void solution(int);

int main(){

int length;

while (true){

cout << "Type in the length of the Fibonacci:";

cin >> length;

if (length <= 0)

cout << "The length must be a positive number. Try again."<< endl;

else break;

10
solution (length);

return 0;

void solution(int a){

int Fibonacci[a];

if (a>0) Fibonacci[0]=0;

if (a>1) Fibonacci[1]=1;

for (int i=2; i<a; i++){

Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1];

cout << "The Fibonacci is: ";

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

cout << Fibonacci[i]<<" ";

Exercise 13:
#include <iostream>

using namespace std;

int main(){

double hourly_rates[5]={9.5, 6.4, 12.5, 5.5, 10.5};

double working_hours[5];

double wages[5];

cout << "Enter the working_hours of 5 staff "<<endl;

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

cout << "Staff number "<<i+1<<": ";

cin >> working_hours[i];

11
wages[i] = working_hours[i]*hourly_rates[i];

cout << "\n\t" <<"Hourly Rates\t"<< "Working Hours\t"<< "Wages\n"

<<"\t--------------------------------------"<<endl;

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

cout <<'\t'<< hourly_rates[i]<<"\t\t"<<working_hours[i]<<"\t\t"<<wages[i]<<endl;

return 0;

Exercise 14:
#include <iostream>

#include <cstring>

using namespace std;

int main() {

char str1[100], str2[100], str3[100];

cout << "Enter the first string: ";

cin >> str1;

cout << "Enter the second string: ";

cin >> str2;

cout << "Enter the third string: ";

cin >> str3;

if (strcmp(str1, str2) > 0) {

swap(str1, str2);

12
}

if (strcmp(str2, str3) > 0) {

swap(str2, str3);

if (strcmp(str1, str2) > 0) {

swap(str1, str2);

cout << "\nStrings in alphabetical order:\n";

cout << str1 << endl;

cout << str2 << endl;

cout << str3 << endl;

return 0;

Exercise 15:
#include<iostream>

#include<string>

using namespace std;

const int MAX = 100;

struct student{

char name[20];

long int rollno;

char sex;

float height;

float weight;

};

int main(){

13
student cls[MAX];

int i,n;

cout << " How many names ? \n";

cin >> n;

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

cout << "record = "<< i+1 << endl;

cout << "name : "; cin>> cls[i].name;

cout << "rollno : "; cin>> cls[i].rollno;

cout << "sex : "; cin>> cls[i].sex;

cout << "height : "; cin>> cls[i].height;

cout << "weight : "; cin>> cls[i].weight;

cout << endl;

// Task a

cout << "Name\t"<< "Rollno\t"<<"Sex\t"<<"Height\t"<<"Weight\t\n";

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

cout <<cls[i].name<<"\t"<< cls[i].rollno <<"\t"<< cls[i].sex <<"\t"<<cls[i].height<<"\


t"<<cls[i].weight<<endl;

// Task b

float Average_Height, Average_Weight;

float Sum_Height=0.0, Sum_Weight=0.0;

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

Sum_Height+=cls[i].height;

Sum_Weight+=cls[i].weight;

Average_Weight=Sum_Weight/n;

Average_Height=Sum_Height/n;

cout <<"The average weight is: "<< Average_Weight<<endl;

14
cout <<"The average height is: "<< Average_Height<<endl;

return 0;

15

You might also like