LAB4
LAB4
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;
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>
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];
}
};
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>
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);
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>
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>
int main() {
double hourly_rates[5] = {9.5, 6.4, 12.5, 5.5, 10.5};
double working_hours[5];
double wages[5];
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
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;
}