0% found this document useful (0 votes)
0 views

PF LAB 12

this is my work
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

PF LAB 12

this is my work
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cin.getline vs cin.

get
Garbage value in char array?

#include <iostream>
using namespace std;

int main()
{
int age;
char name[4];

cout << "Enter your age: ";


cin >> age; // Reads number
cin.ignore(); // Clears the newline character
cin>>name;
cout<<"output :"<<name[4]; //
cin.ignore();
cout << "Enter your full name: ";
char arr[3];
cin.getline(arr, 3); // Reads the full line
cout<<"arr "<<arr;

cin.ignore();
char arr2[3];
cin.getline(arr2, 3); // Reads the full line
cout<<endl<<"arr2 "<<arr2;

return 0;
}

Ifstream: file read


Ofstream: write

void addStudent() {
ofstream outFile("students.txt", ios::app); // Open file in append mode
if (!outFile) {
cout << "Error opening file!" << endl;
return;
}

string name;
int rollNo, marks;
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Marks: ";
cin >> marks;

outFile << rollNo << " " << name << " " << marks << endl; // Write to file
outFile.close();
cout << "Student record added successfully!" << endl;
}

// Function to display all student records


void displayRecords() {
ifstream inFile("students.txt");
if (!inFile) {
cout << "Error opening file!" << endl;
return;
}

string line;
cout << "\nStudent Records:" << endl;
while (getline(inFile, line)) {
cout << line << endl; // Display each line in the file
}

inFile.close();
}
#include <iostream>
#include <cmath>
#include <iomanip>

// Function prototypes
bool isPrime(int n);
float formula1(float n);
float formula2(float n);
void processArray(float arr[], int size);

int main() {
// Declare an array of floating-point numbers (n > 10)
const int n = 12;
float arr[n] = {2, 4, 3, 5, 7, 6, 8, 9, 11, 13, 14, 17};
// Display the array before processing
std::cout << "Array before processing: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

// Process the array


processArray(arr, n);

// Display the array after processing


std::cout << "Array after processing: ";
for (int i = 0; i < n; i++) {
std::cout << std::fixed << std::setprecision(6) << arr[i] << " ";
}
std::cout << std::endl;

return 0;
}

// Function to check if a number is prime


bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= std::sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

// Formula 1: (1 - n^2) / (n + 2)
float formula1(float n) {
return (1 - n * n) / (n + 2);
}

// Formula 2: (n - (1/2 + n)) / (2n + 2)


float formula2(float n) {
return (n - (0.5 + n)) / (2 * n + 2);
}

// Function to process the array


void processArray(float arr[], int size) {
for (int i = 0; i < size; i++) {
if (isPrime(static_cast<int>(arr[i]))) {
// If the item is prime, halve it and apply formula1
float halved = arr[i] / 2.0;
arr[i] = formula1(halved);
} else {
// If the item is not prime, apply formula2
arr[i] = formula2(arr[i]);
}
}
}

You might also like