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

Assignment 2

The document contains a C++ program that demonstrates code structure, including the use of pointers, structs, and a linked list. It initializes variables and prints their values, as well as implements a linked list class to manage nodes with additional information. The program concludes with a sample linked list creation and printing its contents.

Uploaded by

olamohammed2101
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)
0 views

Assignment 2

The document contains a C++ program that demonstrates code structure, including the use of pointers, structs, and a linked list. It initializes variables and prints their values, as well as implements a linked list class to manage nodes with additional information. The program concludes with a sample linked list creation and printing its contents.

Uploaded by

olamohammed2101
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/ 9

Ola Osman MohamedAhmed Osman

21-512
Mathematics + Computer Science

Assignment 2.

1/ Code structure:

#include <iostream>
#include <string>

// Define the data types


int i = 10;
float f = 3.14;

struct Human {
float weight;
std::string gender;
};

// Define pointers for the variables


int* p_i = &i;
float* p_f = &f;
Human h1 = {70.5, "Male"}; // Initialize Human struct correctly
Human* p_h1 = &h1;

int main() {
// Change the value of the integer using pointer dereferencing
*p_i = 20;

// Print statements for all defined variables


std::cout << "The value " << i << " is stored in the memory address " << p_i << std::endl;
std::cout << "The value " << f << " is stored in the memory address " << p_f << std::endl;
std::cout << "The weight " << h1.weight << " and gender " << h1.gender << " are stored in the memory
address " << p_h1 << std::endl;

// Create another human struct and print its values


Human h2 = {65.2, "Female"}; // Initialize Human struct correctly
std::cout << "The weight " << h2.weight << " and gender " << h2.gender << " are stored in the memory
address of h2" << std::endl;

// Adding another variable to the node struct in the linked list class
struct Node {
int data;
Node* next;
// New variable added
std::string additionalInfo;
};

class LinkedList {
public:
void printList(Node* node) {
while (node != nullptr) {
std::cout << node->data << " - " << node->additionalInfo << std::endl;
node = node->next;
}
}
};

// Sample linked list creation and printing


Node* head = new Node();
Node* second = new Node();
Node* third = new Node();

head->data = 1;
head->additionalInfo = "First Node";
head->next = second;

second->data = 2;
second->additionalInfo = "Second Node";
second->next = third;

third->data = 3;
third->additionalInfo = "Third Node";
third->next = nullptr;

LinkedList ll;
ll.printList(head);

return 0;
}
2/ Screenshots for it and for the output :

You might also like