0% found this document useful (0 votes)
15 views2 pages

Lab 6

This document contains solutions to two coding problems involving dynamic memory allocation and dynamic arrays in C++. Solution #1 asks the user to input multiple names, stores the names in a dynamically allocated array, sorts the array, and prints the sorted list. Solution #2 prompts the user to enter the size of an integer array, allocates memory for the array dynamically, takes input from the user to fill the array, and prints the array before deallocating the memory. Both solutions demonstrate how to dynamically allocate and manage memory for arrays at runtime in C++.

Uploaded by

snoop
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)
15 views2 pages

Lab 6

This document contains solutions to two coding problems involving dynamic memory allocation and dynamic arrays in C++. Solution #1 asks the user to input multiple names, stores the names in a dynamically allocated array, sorts the array, and prints the sorted list. Solution #2 prompts the user to enter the size of an integer array, allocates memory for the array dynamically, takes input from the user to fill the array, and prints the array before deallocating the memory. Both solutions demonstrate how to dynamically allocate and manage memory for arrays at runtime in C++.

Uploaded by

snoop
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/ 2

Time: 1.5 hrs.

CS 102 (Lab) Marks: 10


Lab # 06 – Dynamic
Section: C Memory Allocation & Dated: 24/02/2020
Dynamic Arrays

Solution # 01
S:1

#include <algorithm> // For std::sort


#include <iostream>
#include <string>

int getNameCount()
{
std::cout << "How many names would you like to enter? ";
int length{};
std::cin >> length;

return length;
}

// Asks user to enter all the names


void getNames(std::string* names, int length)
{
for (int i{ 0 }; i < length; ++i)
{
std::cout << "Enter name #" << i + 1 << ": ";
std::cin >> names[i];
}
}

// Prints the sorted names


void printNames(std::string* names, int length)
{
std::cout << "\nHere is your sorted list:\n";

for (int i{ 0 }; i < length; ++i)


std::cout << "Name #" << i + 1 << ": " << names[i] << '\n';
}

int main()
{
int length{ getNameCount() };

// Allocate an array to hold the names


auto* names{ new std::string[static_cast<std::size_t>(length)]{} };

getNames(names, length);
// Sort the array
std::sort(names, names + length);

printNames(names, length);

// don't forget to use array delete


delete[] names;
// we don't need to set names to nullptr/0 here because it's going to go out
// of scope immediately after this anyway.

return 0;
}

Solution # 02
S2:

#include<iostream>
using namespace std;

int main()
{

int size, i;
int* ptr;

cout << "\nEnter size of Array : ";


cin >> size;

ptr = new int[size];


//Creating memory at run-time and return first byte of address to ptr.

for (i = 0; i < size; i++) //Input arrray from user.


{
cout << "\nEnter any number : ";
cin >> ptr[i];
}

cout << "\n";


for (i = 0; i < size; i++) //Output arrray to console.
cout << ptr[i] << ", ";

delete[] ptr;
//deallocating all the memory created by new operator

return 0;
}

You might also like