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

lab4

The document contains multiple exercises from a lab assignment focusing on various programming tasks in C++. Each exercise includes code snippets that perform different operations such as finding the maximum of an array, calculating the average, working with exponents, transposing matrices, calculating pi, and managing student records. The exercises cover fundamental programming concepts and data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

lab4

The document contains multiple exercises from a lab assignment focusing on various programming tasks in C++. Each exercise includes code snippets that perform different operations such as finding the maximum of an array, calculating the average, working with exponents, transposing matrices, calculating pi, and managing student records. The exercises cover fundamental programming concepts and data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab 4

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;
}

cout<<"max of array is: "<<max<<endl;

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;

int pos_exp(int x, int n) {


int val = 1;
for (int i = 0; i < n; i++) {
val *= x;
}
return val;
}

float neg_exp(int x, int n) { return 1.0 / pos_exp(x, n); }


int main() {
int x, n;

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;

int arr[n][n], Tarr[n][n];


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "element at row " << i << " and column " << j << "
cin >> arr[i][j];
Tarr[j][i] = arr[i][j];
}
}

cout << "this is the initial matrix" << endl;


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}

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;

float a(int n) { return (pow(-1, n % 2)) / (2 * n + 1); }


int main() {
int n = 0;
float sum = 0;

while (abs(a(n)) > (1.0E-6)) {


sum += a(n);
n++;
}

cout << 4 * sum;


return 0;
}

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);
}

float a(int n) { return 1.0 / frac(n); }


int main() {
int n = 0;
float sum = 0;

while (a(n) > (1.0E-6)) {


sum += a(n);
n++;
}
cout << sum;
return 0;
}

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;
}

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

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;
}

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


}

int main() {
int n = 0;

Lab 4 8
cout << "enter n: ";
cin >> n;

int fib_arr[n];

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


fib_arr[i] = fib(i + 1);
}

cout<<"the array of fibonacci numbers: ";


for (int x: fib_arr){
cout<<x<<" ";
}
return 0;
}

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];

cout << "fill in working_hours: ";


for (double &x : working_hours) {
cin >> x;
}

cout << "hourly_rates: ";


for (double x : hourly_rate) {

Lab 4 9
cout << x << " ";
}

cout << "\nworking_hours: ";


for (double x : working_hours) {
cout << x << " ";
}

cout << "\nwages: ";


for (int i = 0; i < 5; i++) {
wages[i] = working_hours[i] * hourly_rate[i];
cout << wages[i] << " ";
}
return 0;
}

Exc 14
// Exc 14: string sort
#include <iostream>
#include <string>
using namespace std;

void str_sort(string a, string b, string c) {


if (a.compare(b) >0) {
if (b.compare(c) >0) {
cout<<c<<" "<<b<<" "<<a;
}
if (a.compare(c) <0) {
cout<<b<<" "<<a<<" "<<c;
}
}

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;

const int MAX = 100;

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;

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


cin >> n;

// 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;

cout << "---------- --------- ----- ------- -------" << endl

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


cout << setw(10) << cls[i].name
<< setw(10) << cls[i].rollno
<< setw(6) << cls[i].sex
<< setw(8) << cls[i].height
<< setw(8) << cls[i].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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy