0% found this document useful (0 votes)
19 views14 pages

LAB4

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)
19 views14 pages

LAB4

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

Write and run a program that inputs an array of N real numbers, and then finds the

largest element in the array. N should be an input parameter.


#include <iostream>
using namespace std;

int main(){
int N;
float max;
cout << "Type parameter N: ";
cin >> N;
float list[N];
for (int i = 0; i < N; i++){
cout << "Type in value #" << (i + 1) << " of array: ";
cin >> list[i];
if (i == 0){
max = list[i];
}
if (list[i] > max){
max = list[i];
}
}
cout << "The largest value of the array is: " << max;
return 0;
}
Write and run a program that inputs an array of N real numbers, and then computes the
average value of the array elements. N should be an input parameter.

#include <iostream>
using namespace std;

int main(){
int N;
float sum, avg;
cout << "Type parameter N: ";
cin >> N;
float list[N];
for (int i = 0; i < N; i++){
cout << "Type in value #" << (i + 1) << " of array: ";
cin >> list[i];
sum += list[i];
}
avg = sum / N;
cout << "The average value of the array is: " << avg;
return 0;
}
Write and run a program that computes x raised to the power n by repetitive
multiplication. Then modify your program to calculate x raised to the power (-n).
#include <iostream>
using namespace std;

double pow(int b, int exp){


double pow = 1.0;
for (int i = 0; i < exp; i++){
pow *= b;
}
return pow;
}

double negpow(int b, int negexp){


double negpow = 1.0;
for (int i = 0; i < negexp; i++){
negpow *= b;
}
negpow = 1.0 / negpow;
return negpow;
}

int main(){
int x, n;
cout << "Type x for base and n for exponent: \n";
cin >> x >> n;
if (n > 0){
cout << "x^n = " << pow(x, n);
} else if (n == 0){
cout << "x^n = " << 1;
} else {
cout << "x^n = " << negpow(x, -n); //convert n to positive for
negpow
}
return 0;
}
Write and run a program to read a list of real numbers and then find the number of
positive values and the number of negative ones among them. The number of entries is
also entered by the user.

#include <iostream>
using namespace std;

int main(){
int N, posCount = 0, negCount = 0, zeroCount = 0;
cout << "Type parameter N: ";
cin >> N;
float list[N];
for (int i = 0; i < N; i++){
cout << "Type in value #" << (i + 1) << " of array: ";
cin >> list[i];
if (list[i] > 0){
posCount++;
} else
if (list[i] < 0){
negCount++;
} else{
zeroCount++;
}
}
cout << "The number of positive numbers is: " << posCount << endl
<< "The number of positive numbers is: " << negCount << endl
<< "The number of zeros is: " << zeroCount << endl;
return 0;
}
Write and run a program that inputs an integer matrix of order n and transposes it and
then prints it out. Transposing a square matrix means: aij ↔ aji for all i, j.
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
int n;
cout << "Type size of square matrix: ";
cin >> n;
int mat[n][n], transpose[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << "Type element [" << i << "][" << j << "] of the
matrix: ";
cin >> mat[i][j];
}
};

cout << "Original matrix: \n";


for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << setw(4) << mat[i][j] << " ";
transpose[i][j] = mat[j][i]; //transpose the matrix
};
cout << endl;
};

cout << "Transposed matrix: \n";


for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << setw(4) << transpose[i][j] << " ";
};
cout << endl;
};
return 0;
}
#include <iostream>
#include <cmath>

using namespace std;

int main(){
int N = 0;
float Approx = 0.0, preApprox = 0.0;
do{
preApprox = Approx;
Approx += pow(-1.0, N) / (2 * N + 1);
N++;
} while (fabs(Approx - preApprox) >= 1.0E-6);
cout << Approx * 4.0 << endl << "Times: " << N;
return 0;
}
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
//header
cout << " x(degrees) | sin(x) | cos(x) | tan(x) " <<
endl;
cout << "------------|------------|------------|------------" <<
endl;
cout << setprecision(4) << fixed;
for (int x = 5; x <= 85; x += 5) {
// Convert degrees to radians
double radian = x * M_PI / 180.0;
double sin_x = sin(radian);
double cos_x = cos(radian);
double tan_x = tan(radian);

cout <<
" " << setw(2) << x << " | " << sin_x << " | " <<
cos_x << " | " << tan_x << endl;
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;

//calculate factorial
double factorial(int n) {
double fact = 1.0;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}

int main() {
double eApprox = 1.0;
double prevApprox = 0.0;
int n = 0; //counter

do {
prevApprox = eApprox;
eApprox += 1.0 / factorial(n + 1);
n++;
} while (fabs(eApprox - prevApprox) >= 1.0E-6);

cout << eApprox << endl


<< "Times: " << n << endl;
return 0;
}
#include <iostream>

using namespace std;

int main() {
int n;
cout << "Enter a number n for the nth Fibonacci number: ";
cin >> n;
if (n == 0) return 0;
if (n == 1) cout << "Fibonacci number at position 1 is: 1" << endl;

long a = 0, b = 1, next;
//calculate the nth number
for (int i = 2; i < n; i++)
{
next = a + b;
a = b;
b = next;
}
// Calculate and print the nth Fibonacci number
cout << "Fibonacci number at position " << n << " is: " << b << endl;
return 0;
}
#include <iostream>

using namespace std;

int main() {
int n;
cout << "Enter array size n: ";
cin >> n;
if (n < 2) return 0;

long a = 0, b = 1, next;
long fib[n];
fib[0] = 0;
fib[1] = 1;
//calculate the nth number
for (int i = 0; i < n; i++) {
next = a + b;
a = b;
b = next;
fib[i + 2] = b;
cout << fib[i] << " ";
}
return 0;
}
#include <iostream>
#include <iomanip>

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 for 5 jobs:\n";


for (int i = 0; i < 5; ++i) {
cout << "Job " << (i + 1) << " - Hours: ";
cin >> working_hours[i];
}

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


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

cout << " Hourly Rate | Working Hours | Wages \n";


cout << "-------------------------------------\n";

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


cout << fixed << setprecision(2) << setw(9) << hourly_rates[i]
<< fixed << setprecision(2) << setw(12) << working_hours[i]
<< fixed << setprecision(2) << setw(15) << wages[i] << endl;
}
return 0;
}
#include <iostream>
#include <cstring>

using namespace std;


int main() {
char str1[100], str2[100], str3[100]; //maxchar is 100
char temp[100]; //for swapping
cout << "Enter the first string: ";
cin.getline(str1, 100);
cout << "Enter the second string: ";
cin.getline(str2, 100);
cout << "Enter the third string: ";
cin.getline(str3, 100);

if (strcmp(str1, str2) > 0) { // Swap str1 and str2 if str1 > str2
for (int i = 0; i < 100; i++) {
temp[i] = str1[i];
str1[i] = str2[i];
str2[i] = temp[i];
}
}
if (strcmp(str2, str3) > 0) { // Swap str2 and str3 if str2 > str3
for (int i = 0; i < 100; i++) {
temp[i] = str2[i];
str2[i] = str3[i];
str3[i] = temp[i];
}
}
if (strcmp(str1, str2) > 0) { // Swap str1 and str2 again if str1 >
str2

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


temp[i] = str1[i];
str1[i] = str2[i];
str2[i] = temp[i];
}
}
cout << "\nStrings in alphabetical order:\n";
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Exercise 15:
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


const int MAX = 100;
struct student {
char name[20];
long int rollno;
char sex;
float height;
float weight;
};

int main() {
student cls[MAX];
int i, n;
cout << "How many names? \n";
cin >> n;
for(i = 0; i < n; ++i) {
cout << "Record = " << i + 1 << endl;
cout << "Name: ";
cin >> cls[i].name;
cout << "Roll No: ";
cin >> cls[i].rollno;
cout << "Sex: ";
cin >> cls[i].sex;
cout << "Height: ";
cin >> cls[i].height;
cout << "Weight: ";
cin >> cls[i].weight;
cout << endl;
}
cout << " Name Rollno Sex Height Weight" << endl;
for(i = 0; i < n; i++) {
cout << setw(10) << cls[i].name << " "
<< setw(3) << cls[i].rollno << " "
<< setw(3) << cls[i].sex << " "
<< setw(5) << cls[i].height << " "
<< setw(5) << cls[i].weight << " " << endl;
}
return 0;
}

You might also like