lab4
lab4
Exc 3:
// Exc 3: max of array
#include <iostream>
using namespace std;
int main() {
int n, max = 0;
cout<<"enter n: ";
cin>>n;
int arr[n];
for (int i=0; i<n;i++) {
cout<<"element "<<i<<" of the array: ";
cin>>arr[i];
(max < arr[i])? max = arr[i]: max;
}
return 0;
}
Exc 4
// Exc 4: average of array
#include <iostream>
using namespace std;
int main() {
Lab 4 1
int n, sum = 0;
cout << "enter n: ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cout << "element " << i << " of the array: ";
cin >> arr[i];
sum += arr[i];
}
cout << "average of array is: " << ((float)sum) / n << endl;
return 0;
}
Exc 5
// Exc 5: exponent
#include <iostream>
using namespace std;
Lab 4 2
cout << "enter x: ";
cin >> x;
cout << "enter n: ";
cin >> n;
cout << "x raised to the power of n: " << pos_exp(x, n) << end
cout << "x raised to the power of -n: " << neg_exp(x, n) << en
return 0;
}
Exc 6
// Exc 6: pos or neg
#include <iostream>
using namespace std;
int main() {
int n, pos = 0, neg = 0;
cout << "enter n: ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cout << "enter element " << i << " of the array: ";
cin >> arr[i];
if (arr[i] < 0) {
neg += 1;
}
if (arr[i] > 0) {
pos += 1;
}
}
Lab 4 3
cout << "number of positive numbers: " << pos << endl;
cout << "number of negative numbers: " << neg << endl;
return 0;
}
Exc 7
// Exc 7: transpose the square matrix
#include <iostream>
using namespace std;
int main() {
int n;
cout << "enter n: ";
cin >> n;
Lab 4 4
cout << "this is the transposed matrix" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << Tarr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Exc 8
// Exc 8: calculate pi
#include <cmath>
#include <iostream>
using namespace std;
Lab 4 5
Exc 9
// Exc 9: tabulate trigonometry
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
float x;
const float PI = 3.14159265358;
cout << "x sin(x) cos(x) tan(x)" << endl;
for (int i = 1; i <= 17; i++) {
x = i * 5;
cout << x << " " << sin((x * PI / 180)) << " " << cos((x * P
<< tan((x * PI / 180)) << endl;
}
return 0;
}
Exc 10
// Exc 10: calculate e
#include <cmath>
#include <iostream>
using namespace std;
int frac(int n) {
if (n == 0) {
return 1;
}
Lab 4 6
return n * frac(n - 1);
}
Exc 11
// Exc 11: fibonacci
#include <cmath>
#include <iostream>
using namespace std;
int fib(int n) {
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
Lab 4 7
}
int main() {
int n = 0;
cout << "enter n: ";
cin >> n;
cout << "the " << n << "(th) fibonacci number is " << fib(n);
return 0;
}
Exc 12
// Exc 12: fibonacci array
#include <cmath>
#include <iostream>
using namespace std;
int fib(int n) {
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
int main() {
int n = 0;
Lab 4 8
cout << "enter n: ";
cin >> n;
int fib_arr[n];
Exc 13
// Exc 13: wages
#include <iostream>
using namespace std;
int main() {
double hourly_rate[5] = {9.5, 6.4, 12.5, 5.5, 10.5};
double working_hours[5], wages[5];
Lab 4 9
cout << x << " ";
}
Exc 14
// Exc 14: string sort
#include <iostream>
#include <string>
using namespace std;
Lab 4 10
else {
if (b.compare(c) <0) {
cout<<a<<" "<<b<<" "<<c;
}
if (a.compare(c) >0) {
cout<<c<<" "<<a<<" "<<b;
}
}
}
int main() {
string a,b,c;
cin>>a>>b>>c;
str_sort(a,b,c);
return 0;
}
Exc 15
//Exc 15: class table
#include <iostream>
#include <iomanip> // for std::setw
using namespace std;
struct student {
char name[20];
long int rollno;
char sex;
float height;
float weight;
};
int main() {
Lab 4 11
student cls[MAX];
int i, n;
float totalHeight = 0, totalWeight = 0;
// Input records
for (i = 0; i < n; ++i) {
cout << "Record = " << i + 1 << endl;
cout << "Name: "; cin >> cls[i].name;
cout << "Rollno: "; cin >> cls[i].rollno;
cout << "Sex (M/F): "; cin >> cls[i].sex;
cout << "Height: "; cin >> cls[i].height;
cout << "Weight: "; cin >> cls[i].weight;
totalHeight += cls[i].height;
totalWeight += cls[i].weight;
cout << endl;
}
// Display data
cout << "\nData of students:\n";
cout << setw(10) << "Name"
<< setw(10) << "Rollno"
<< setw(6) << "Sex"
<< setw(8) << "Height"
<< setw(8) << "Weight" << endl;
Lab 4 12
}
// Compute averages
float avgHeight = totalHeight / n;
float avgWeight = totalWeight / n;
// Display averages
cout << "\nAverage height: " << avgHeight << endl;
cout << "Average weight: " << avgWeight << endl;
return 0;
}
Lab 4 13