Starting from:
$35

$29

Data Structures Reecitation 1 Solved


A Variable is a location inn a computer memory used by the program whicch can be accessed by their name. This avvoids the need for the programmer to remember the physical address of the data in memory a nd instead use the variable to refer to the data when needed.

In the below example, we see that we are storing 10 in a variable named a, and storing 20 in a variable named b. The program has no reference to any physical memory addreess, and instead, all operations on the data 10 andd 20 are done using the variables a, and b.

















Note that the code when executeed on multiple machines, or at different points inn time, allocates different physical memory addresses. The beauty of variables is that YOU as a programmer do not have to remember the locatioons of the data. You can focus on the code-logic instead.
CSCI 2270 – Data Structures

Reecitation 1, Spring 2022


2. Compiling from coommand line

Consider we want to compile and execute the following hello.cpp. Assume hello.cpp is within folder lab1

File: hello.cpp












    1. Compiling and Executing the file in the Terminal (Command Line)

        a. Open a terminal in your environment. If you are using a linux bassed system open terminal in your machine. If you are using csel.io open terminal from file-->new-- >terminal

        b. Navigate to the lab1 folder in the terminal with cd commands.

        c. Run the following command to compile the cpp file (ensure wsl is selected as the terminal)




Here,

    1. ‘g++’ is the name of the compiler program.

    2. The ‘-std=c++11’ option tells the compiler to use the 2011 version of C+++.

    3. ‘hello.cpp’ is the file to bee compiled.

    4. ‘-o hello’ tells the comppiler to write its output to a file named ‘helloo’ (‘hello.exe’ on Windows). If this is missing, the output file will be named ‘a.out’ or ‘a’ by default.

    5. If the last command wa s successful, there should now be another fiile named ‘hello’ (‘hello.exe’ on Windows).

    6. To run the program, run the command ‘./hello’ (or simply hello on Windows).
CSCI 2270 – Data Structures

Reecitation 1, Spring 2022




3. Functions

One of the principles of software development is DRY (Do not Repeat Yourself) aimed at reducing repeated code. One waay to achieve this is by using functions. For exammple, if we would like to add two numbers, and call it multiple times, we can create a functioon called add.

We do this by creating sepaarate header file to declare the function (too avoid another programmer to rewrite the function signature). We start with the first of the threee files.

Here, we define the function decclaration. Saying, we have a function called addd and it takes two integer arguments as input.

File: function.h




In another file, we define the function by adding logic into it.

File: funcdef.cpp










Then, we call the declared function multiple times on as many different valuess of inputs as we need. Moreover, the header and function definition files can be shareed with multiple programmers. Notice that we have only included the header file in the main cpp file.

File: main.cpp

CSCI 2270 – Data Structures

Reecitation 1, Spring 2022



To compile multiple files, we passs only the cpp files as shown below.




Handling command line arguments


main is also a function. Can it take arguments? If so then how can we provide thhose arguments? The answer is commmand line arguments.

Often, we would like our code to take multiple arguments as input and process it accordingly. This is done by modifying the siggnature of the main function from this






To this.






Notice the change in the function signature. It now accepts two parameters: argc and argv. argcstores the count of the total number of arguments you have passed in the command-line on execution, and the second one argvis an array of strings storing the argumennts passed in the command-line. The following program reads an arbitrary number of arguments from the command line and prints them as output.

File: commandLine.cpp

CSCI 2270 – Data Structures

Reecitation 1, Spring 2022


Compile the above code followinng the same syntax as mentioned before in this document.







Example1 : Simulating no arguuments

To simulate no arguments, execute the code as follows (pass no arguments)






Here, the main function only receives one argument, which is the name of th e program itself. Thus, argc is one, and argv is an array of length 1, where the only element in this array is a string “./commandLine”.

Example2 : More arguments

We can pass multiple argumennts by typing each one after the function namme, separated by spaces. So, if we run the progrram using the command (we pass 3 strings called arg1, arg2, arg3)






Now argc is 4 and argv is an array of length 4. The first string in the array is thhe program name “./commandLine”, and the rest of them are the strings we typed on the terminal, delimited by spaces or tab.

4. File I/O

Some programs require reading data from files to process them, and some programs process input and outputs large amount of data which cannot be perused if printed to the terminal. C++ allows File I/O (input/output) by using ifstream and ofstream for readiing and writing, respectively.

You replace cinand coutoperatoors with the following for reading and writing.

First, you declare an instance off file input (the file from which you read the data). Note that this file must be in the same directory as the code executable. Otherwise, you must provide a fully qualified path name as the argumment.
CSCI 2270 – Data Structures

Reecitation 1, Spring 2022






Similarly, to output data into a file, you must declare an instance of file output. By default, the file will be in the same directory as the code executable. Otherwise, you must provide a fully qualified path name as the argumment.






We can provide an additional arrgument for output, whether to append at the end of the file, or overwrite the file before printing. Some of those operation modes are given beloow.

File operation modes:









File mode example:






In the next page, we have providded sample programs for file input and file outpuut.
CSCI 2270 – Data Structures

Reecitation 1, Spring 2022


File output example - oFile.cpp

On compilation and execution, check for the newly created “filename.txt”



























File input example - iFile.cpp

CSCI 2270 – Data Structures

Reecitation 1, Spring 2022


5. Structs


Sometimes, to represent a real-world object, simple datatypes are not enough. To represent a Student, we need some (if not all) of the follo wing: age, gender, date-of-birth, name, address etc.

You could write code as follows:









This becomes increasingly complex if we would like to store multiple Student representations. One way to solve this is by using aggregated(ggrouped) user-defined datatype called Struct. This datatype can hold different datatypes grouped under a common variable of type Struct.















Exercise

Your zipped folder for this Lab will have a main.cpp, addStudent.cpp and addStudent.hpp files. Follow the TODOs in these files to complete yoour Recitation exercise!

The task of main function will be:

    1. Take filename as commandd line argument

    2. Open the file

    3. Create a student array.

    4. Read data from file and calll the function addAstudent to add the student in the array

The function addAstudent can be foound in addStudent.cpp. It will do the following

    1. It will receive the name, emmail and birthday of the student as arguments. It will also receive the array and the index as argu ments.

    2. It will add a student record in the array at specified index.

    3. It will return the next index of the array

Zip your solution files and upload it to the Recitation 1 Dropbox within Canvas.

More products