NMĐT Lab 4
NMĐT Lab 4
void solution(int);
int main(){
int length;
while (true){
if (length <= 0)
cout << "The length must be a positive number. Try again."<< endl;
else break;
solution (length);
return 0;
float RangeOfNum[a];
float test;
test = RangeOfNum[0];
1
for (int i=0; i<a; i++){
test = RangeOfNum[i];
cout << "The largest element in the array is: "<< test;
Exercise 4:
#include <iostream>
void average(int);
int main()
int length;
while (true){
if (length <= 0)
cout << "The length must be a positive number. Try again."<< endl;
else break;
average(length);
return 0;
float RangeOfNum[a];
2
float result;
sum += RangeOfNum[i-1];
result = sum/a;
cout << "The average value of the array is: "<< result;
Exercise 5:
#include <iostream>
#include <math.h>
int main()
{ double base;
int exponent;
cout << base << " to the power of " <<exponent<<" is " << solution(base,exponent);
3
double solution(double a, int b){
double result=1.0;
result *= a;
if (b<0)
result = 1/result;
return result;
Exercise 6:
#include <iostream>
void solution(int);
int main(){
int length;
while (true){
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;
if (RangeOfNum[i]=0){
continue;
else if (RangeOfNum[i]>0){
PostCount++;
else NeCount++;
Exercise 7:
#include <iostream>
void solution(int);
int main(){
int length;
while (true){
5
cout << "Type in the order of the array:";
if (length <= 0)
cout << "The order must be a positive number. Try again."<< endl;
else break;
solution (length);
return 0;
cout << "Enter the elements of the matrix row by row:" << endl;
transposed[j][i] = matrix[i][j];
6
}
Exercise 8:
#include <iostream>
#include <math.h>
int main(){
double ApproximateValue=0.0;
int n=0;
PreviousValue = ApproximateValue;
ApproximateValue += term;
n++;
return 0;
Exercise 9:
#include <iostream>
#include <math.h>
#define PI 3.141592653589793
7
int main() {
int x=5;
//Start to calculate
//Convert to radians
//Tabulate
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>
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
8
int main() {
int n = 1;
previousApproximation = approximateE;
n++; /
cout << "The approximate value of e is: " << approximateE << endl;
return 0;
Exercise 11:
#include <iostream>
if (n == 0) return 0;
if (n == 1) return 1;
int main() {
int n;
cout << "Enter the position (n) of the Fibonacci sequence: ";
cin >> n;
9
if (n < 0) {
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
void solution(int);
int main(){
int length;
while (true){
if (length <= 0)
cout << "The length must be a positive number. Try again."<< endl;
else break;
10
solution (length);
return 0;
int Fibonacci[a];
if (a>0) Fibonacci[0]=0;
if (a>1) Fibonacci[1]=1;
Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1];
Exercise 13:
#include <iostream>
int main(){
double working_hours[5];
double wages[5];
11
wages[i] = working_hours[i]*hourly_rates[i];
<<"\t--------------------------------------"<<endl;
return 0;
Exercise 14:
#include <iostream>
#include <cstring>
int main() {
swap(str1, str2);
12
}
swap(str2, str3);
swap(str1, str2);
return 0;
Exercise 15:
#include<iostream>
#include<string>
struct student{
char name[20];
char sex;
float height;
float weight;
};
int main(){
13
student cls[MAX];
int i,n;
cin >> n;
// Task a
// Task b
Sum_Height+=cls[i].height;
Sum_Weight+=cls[i].weight;
Average_Weight=Sum_Weight/n;
Average_Height=Sum_Height/n;
14
cout <<"The average height is: "<< Average_Height<<endl;
return 0;
15