0% found this document useful (0 votes)
11 views5 pages

C++ Array and File Handling Examples

The document contains various C++ code snippets demonstrating the use of arrays, dynamic memory allocation, file handling, and type conversion. It illustrates how to declare and manipulate arrays, input and display student records, and perform file operations. Additionally, it explains implicit and explicit type conversions in C++.

Uploaded by

rabiaakhtar1
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)
11 views5 pages

C++ Array and File Handling Examples

The document contains various C++ code snippets demonstrating the use of arrays, dynamic memory allocation, file handling, and type conversion. It illustrates how to declare and manipulate arrays, input and display student records, and perform file operations. Additionally, it explains implicit and explicit type conversions in C++.

Uploaded by

rabiaakhtar1
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

int a[] = {23, 12, 43, 97, 45};

int n = sizeof(a) / sizeof(a[0]);

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


cout << a[i] << ‘\t’;
#include<array>

array<int, 5> a = {23, 12, 43, 97, 45};


int n = [Link]();

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


cout << [Link](i) << ‘\t’;
#include <iostream>
#include <array>
using namespace std;

int main() {
// Declare and initialize a array
array<int, 5> arr = {1, 2, 3, 4, 5};

// Access elements using operator[]


cout << "Elements of the array: ";
for (int i = 0; i < [Link](); ++i) {
cout << arr[i] << " ";
}
cout << endl;

// Access elements using at() (with bounds checking) cout <<


"Element at index 2 (using at()): " << [Link](2) << endl;

// Access the first and last elements


cout << "First element (using front()): " << [Link]() << endl;
cout << "Last element (using back()): " << [Link]() << endl;

// Fill the array with a specific value


[Link](10);
cout << "Array after filling with 10: ";
for (const auto& elem : arr) {
cout << elem << " ";
}
cout << endl;

// Use iterators to access elements


cout << "Elements using iterators: ";
for (auto it = [Link](); it != [Link](); ++it) {
cout << *it << " ";
}
cout << endl;

// Use reverse iterators


cout << "Elements in reverse order: ";
for (auto rit = [Link](); rit != [Link](); ++rit) {
cout << *rit << " ";
}
cout << endl;
// Check if the array is empty
cout << "Is the array empty? " << ([Link]() ? "Yes" : "No") << endl;

// Swap contents of two arrays


array<int, 5> anotherArr = {5, 4, 3, 2, 1};
[Link](anotherArr);
cout << "Array after swapping with another array: ";
for (const auto& elem : arr) {
cout << elem << " ";
}
cout << endl;

return 0;
}
#include <iostream>
#include <string>
using namespace std;

// Define a structure to store student's record


struct Student {
string name;
int rollNumber;
float marks;
};

// Function to input student details


void inputStudentDetails(Student& student) {
cout << "Enter student's name: ";
getline(cin, [Link]);
cout << "Enter roll number: ";
cin >> [Link];
cout << "Enter marks: ";
cin >> [Link];
[Link](); // To consume the newline character left by cin }

// Function to display student details


void displayStudentDetails(const Student& student) {
cout << "Name: " << [Link] << endl;
cout << "Roll Number: " << [Link] << endl;
cout << "Marks: " << [Link] << endl;
}

int main() {
int numStudents;

cout << "Enter the number of students: ";


cin >> numStudents;
[Link](); // To consume the newline character left by cin

// Dynamically create an array of students


Student* students = new Student[numStudents];

// Input details for each student


for (int i = 0; i < numStudents; ++i) {
cout << "Enter details for student " << i + 1 << ":" << endl;
inputStudentDetails(students[i]);
}

// Display details for each student


cout << "\nStudent Records:\n";
for (int i = 0; i < numStudents; ++i) {
cout << "Details of student " << i + 1 << ":" << endl;
displayStudentDetails(students[i]);
cout << endl;
}

// Free the allocated memory


delete[] students;
return 0;
}
#include <iostream>
using namespace std;

int main() {
// Dynamically allocate memory for a single integer
int* ptr = new int;
*ptr = 20;
cout << "Single integer: " << *ptr << endl; // Output: Single integer: 20

// Dynamically allocate memory for an array


int* arr = new int[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
cout << "Array values: ";
for (int i = 0; i < 3; ++i) {
cout << arr[i] << " "; // Output: Array values: 1 2 3
}

// Deallocate memory
delete ptr; // Deallocates memory for a single integer
delete[] arr; // Deallocates memory for the array

return 0;
}
#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream ofile;
[Link]("[Link]"); //Open file as output stream if
(!ofile) {
cerr << "Error opening the file." << endl;
return 1;
}

for(int i=1; i<=10; i++)


ofile << "Line number " << i << endl;
[Link]();
ifstream ifile;
[Link]("[Link]"); //Open file as input stream
if (!ifile) {
cerr << "Error opening the file." << endl;
return 1;
}
string line;
while(getline(ifile, line))
cout << line << endl;

[Link]();
}
#include <iostream>
#include <fstream>
using namespace std;

int main() {
fstream file;
[Link]("[Link]", ios::out); //Open file as output stream
if (!file) {
cerr << "Error opening the file." << endl;
return 1;
}

for(int i=1; i<=10; i++)


file << "Line number " << i << endl;
[Link]();
[Link]("[Link]",ios::in); //Open file as input stream
if (!file) {
cerr << "Error opening the file." << endl;
return 1;
}
string line;
while(getline(file, line))
cout << line << endl;
[Link]();
}
#include <iostream>
#include <fstream>
using namespace std;

int main() {
fstream file;
[Link]("[Link]", ios::out | ios::in); //Open file as both output and input stream if
(!file) {
cerr << "Error opening the file." << endl;
return 1;
}

for(int i=1; i<=10; i++)


file << "Line number " << i << endl;

string line;
[Link](0, ios::beg); //[Link](offset, direction);
while(getline(file, line))
cout << line << endl;
[Link]();
}

Implicit Type Conversion (Automatic Type Conversion)


• The order of promotion from smallest to largest is generally:

o bool → char → short int → int → unsigned int → long → unsigned long → long
long → float → double → long double

This promotion order helps in ensuring no data loss when smaller types are promoted to
larger types in mixed-type expressions.

Explicit Type Conversion (Type Casting)


• Converting by Assignment:

o (type)expression or type(expression)

You might also like