Click the little computer above for a detailed description.
For this excercise you will debug a program and demonstrate the correct
results to your lab instructor.
Replit is an online compiler for C++. If you haven't already, follow these instructions to join the Replit Team for this lab. All lab exercises will be performed and submitted through Replit.
While Replit has several advantages, such as cloud storage and compatibility, there are a few potential problems you should be aware of when using it:
language = "c++" run = "g++ filename1.cpp filename2.cpp -o outputname; ./outputname"
For an example, copy the following simple program to the main.cpp, found in the file menu on the left hand side of your Replit:
Try clicking the run button. The console window on the right should prompt you for your name, and greet you after you have entered it
Alternatively, you could enter the following into the console window:
g++ main.cpp -o hello ./hello
This should compile your main.cpp into an executable, a compiled file that you can "execute" to perform it's defined tasks, and run it. The result should be the same.
You should make sure you are comfortable using the g++ compiler on command line, as some labs in the future will require it.
The preceding section showed the procedure for creating a C++ program with only one file component. However, when you are working on a large program, you would break your program into smaller, more manageable parts. In other words, your project would contain more than one C++ source file. You can create each file yourself, just like you did above, or you can add C++ source files from somewhere else.
The following section demonstrates how to create, compile, and execute a project with more than one file.
Right click on these files and click "save link as" or "save target as" (depending on your browser). Make note of where you save them to.
Once you have the files saved, you can either click and drag them into the files menu on the left of your Replit, or click the dots in the top right and "upload file." You will be prompted to overwrite the main.cpp file - do so.
You can view the contents of these files simply by clicking on them. You should be able to click the run button to view the results because we are compiling a file named "main.cpp". The program should tell you today's date and a fictional birthday.
Next, enter the following lines into the console:
g++ main.cpp date.cpp -o date ./date
Again, this will compile the separate files into a single executable named date, which will run, producing the same output as before.
Like most people, you may find that your programs do not work as you expected the first time you run them. To find out what is wrong, you might use cout statements to follow the line of execution of your code. This works, but later, when things are fixed, you must remember to remove all the extra cout statements.
You can use a debugger to find errors in your code during run-time instead. However, before we get into the details of how to use this debugger, some terminology might be useful.
Syntax Errors |
|
Semantic Errors |
|
Logic Errors |
|
Debugger |
|
Breakpoints |
|
You may find the visual basic reference page https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/error-types helpful for further understanding
As a general overview to debug your project you must:
When your program is stopped at a breakpoint, your program and debugger are said to be in break mode.
To debug a program, use the following steps:
The location: ftp://ftp.cs.uregina.ca/pub/class/210/ftp/VCIntro The file: debug.cpp
Follow these steps to debug debug.cpp program:
language = "c++" run = "g++ debug.cpp -o debug; ./debug"
Examine the code in the source code window, and you will find
that inside the first loop block, the statement
cin >> Values[1];
should read: cin >> Values[i];;
otherwise, all the keyboard input will go to Values[1].
Change this line of code; recompile and rerun the program and it
should work properly.
If you are still having trouble, consider checking out
10 Common Programming Mistakes in C++
A linked list is a data structure that strings together a collection of nodes. Each node typically contains a data item and a pointer to the memory address of the next node in the list. This means that the list must be travelled in order, or sequentially, from front to back, starting at a special node called the head, and there is no way to travel backwards through the list. Here is a graphical representation of a linked list:
A linked list typically has the following functions:
First, create a node initialized with a data value and a NULL pointer. Set the "head" to point to the first node. Set up a current-pointer to the first node (or "head"). Get a data value for the next node. While more nodes to add { Create a new node initialized with the data value and a NULL pointer. Set the current-pointer link member ("next") to the new node. Set the current-pointer to point to the new node. Get a data value for the next node. }
//Find the end of the list... Set a current-pointer to the "head". While current-pointer link member ("next") is not NULL { Set the current-pointer to the "next" node in the list. } //Now current-pointer points to the last node... Create a new node initialized with the "item" and a NULL pointer. Set the current-pointer link member ("next") to this new node.
Set a current-pointer to the "head". While current-pointer is not NULL { Print the data member ("datum") of the current node Set the current-pointer to the "next" node in the list. }
Set a previous-pointer to NULL. Set a current-pointer to the "head". //Find where your want to insert into the list... While the new data member > current-pointer value { //Move both pointers along in the list... Set the previous-pointer to the current-pointer Set the current-pointer to the "next" node in the list. } Create a new node initialized with the "item" and current-pointer. Set the link member in the previous_pointer to the new node.
If "item" is in the first node { Set a delete-pointer to the first node. Change the "head" pointer to the second node. Delete the node indicated by delete-pointer } Else { Set a previous-pointer to the "head". While previous-pointer->next is not equal to NULL and previous-pointer->next->datum is not equal to "item" { Set the previous-pointer to the "next" node in the list. } If you have not reached the end of the list { Set a delete-pointer to the node to be deleted (Note: it is the node after previous-pointer). Set the link member ("next") of the previous-pointer to the "next" node after delete-pointer. Delete the node indicated by delete-pointer. } Otherwise, print a "not found" message }
The files: LinkedList.cpp,LinkedList.h, and main.cpp
Copy these files into the file window on your Replit. Since this file is named "main.cpp", you shouldn't need a .replit configuration file, but if you would like one for reference, you would write:
language = "c++" run = "g++ main.cpp LinkedList.cpp -o ./a.out; ./a.out"
When you are finished, your program should be able to delete items from the list, or provide an error message when the item is not found. Your program must not crash or core dump when the item is not found.
Example output:
|
|
|