Need help understanding some code.

Status
Not open for further replies.

JC0724

Junior Member
Oct 11, 2008
3
0
0
Understanding the problem != having others do your work for yoiu.

Advice has been generated in the thread.

The thread is locked to prevent abuse by people providing you the answers. Study the concepts and possibly post the error condition if desired.

Senior Anandtech Moderator
Common Courtesy




I am just looking for a little bit better understanding on these problems. Meaning that I am not even sure if I did them correctly. Also if this is the correct way to post code in the forums?

1. Consider the following program that Clyde P. Barkwasser, a new student to C++, wrote to practice working with character arrays and stream I/O.
Working with your debugger, please determine if the program accomplishes what Clyde intended to do. If there are any errors, please identify the line or lines that may be in error, identify what the error is, and how you found it with your debugger. Please make the appropriate correction(s) and show that your proposed modification fixes the original problem.



Here is the orginal code:


#include <iostream>
using namespace std;

// this is a simple routine that demonstrates how to fill an array of characters

void main(void)
{
char myArray[5]; // declare a character array

for (int i = 0; i <= 5; i++) // fill array with characters
{
// fill with the ascii characters A..F
// 65 is the ascii value for A

myArray= 65+i;
}

for (i = 0; i <= 5; i++) // display the array
{
cout << myArray;
}

cout << endl;

return;
}



and here is my code


#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;


// this is a simple routine that demonstrates how to fill an array of characters

void main(void) {

char myArray[5]; // declare a character array
int j = 'A'; // initializing J to the char A for debugging

for (int i = 0; i <= 5; i++) // fill array with characters
{
// fill with the ascii characters A..F
myArray= 65+i; // 65 is the ascii value for A
}

for (i = 0; i <= 5; i++) // display the array
{
if (j == myArray) { //testing array to compare characters with ascii values
cout << myArray;
}

else
cout << "program not working"; //if program does not work produce a prompt that says program not working

j++; //incrementing J to next character value

}

cout << endl;

return;

}



3. Clyde's sister, Brunelda Raisondetre, also in the class, is practicing using function prototypes and passing variables to functions. She's written the following program. What do you have to say to Brunelda. Use your debugger to illustrate your analysis. Can you correct her error(s) using things we may have covered in class? Please make the appropriate correction(s) and show that your proposed modification fixes the original problem.
Please note that you are changing what the pointers refer to, not the contents of the containers to which they are referring.


Here is the orginal code.

#include <iostream.h>

void swapPointers (int* aPtr0, int* aPtr1);

void main(void)
{
// declare a couple of arrays
int myArray0[] = {1, 2, 3};
int myArray1[] = {4, 5, 6};

// swap them
swapPointers (myArray0, myArray1);

// test to make sure it worked
for (int i = 0; i < 3; i++)
{
cout << myArray0 << " " << myArray1 << endl;
}

return;
}

// this routine interchanges the pointers to two arrays
// inputs: pointers to two arrays
// outputs: none
// function: accept pointers to two arrays. Interchange them such that the first
// pointer points to the second array and the second pointer to the first.

void swapPointers (int* aPtr0, int* aPtr1)
{
// swap the pointers
int* tempPtr = aPtr0;
aPtr0 = aPtr1;
aPtr1 = tempPtr;

// test it to make sure it works
for (int i = 0; i < 3; i++)
{
cout << aPtr0 << " " << aPtr1 << endl;
}

return;
}


Here is my code

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;





void swapPointers (int* aPtr0, int* aPtr1); // this function interchanges the pointers to two arrays

void main(void)
{

int myArray0[] = {1, 2, 3}; // declare a couple of arrays
int myArray1[] = {4, 5, 6}; // declare a couple of arrays


swapPointers (myArray0, myArray1); // swap them


for (int i = 0; i < 3; i++) // test to make sure it worked
{
cout << myArray0 << " " << myArray1 << endl;
}

return;
}




void swapPointers (int* aPtr0, int* aPtr1) // function: accept pointers to two arrays. Interchange them such that the first
{ // pointer points to the second array and the second pointer to the first.

int* tempPtr = aPtr0; // swap the pointers
aPtr0 = aPtr1;
aPtr1 = tempPtr;
int myDebugArray0[] = {1, 2, 3}; // using my debug array to make sure the values are correct for the swap.
int myDebugArray1[] = {4, 5, 6};
int j = 0; //used to run through myDebugArray for checking for errors


for (int i = 0; i < 3; i++) // test it to make sure it works
{
if (myDebugArray0[j] != aPtr0 && myDebugArray1[j] != aPtr1) { //my debug statement to check if there are any errors
cout << aPtr0 << " " << aPtr1 << endl;
}

else
cout << " program not working \n"; //used to show program not working

j++;
}

return;
}




Also I don't understand this code here much at all to even try to write a testing code(if I am doing it right in the first place.

#include <iostream.h>

// get data from the user
void getData(int* aValuePtr);

void main (void)
{
// declare a shared variable and a pointer to it
int myValue;
int* myPtr = &myValue;

// get data from the user
getData(myPtr);

// display the data
cout << *myPtr << endl;

}

// prompt the user for some data and return it through a shared
// variable pointed to by valuePtr
// inputs: pointer to a container in which to place the data
// outputs: none
// function: the routine accepts a pointer to a container in which to store data from a user,
// it prompts for the data, accepts the data, displays it, and returns

void getData(int* valuePtr)
{
// declare a temp place to store the data
int tempValue;

// let valuePtr point to it
valuePtr = &tempValue;

// prompt for data
cout << "Please enter a value" << endl;

// get the data
cin >> *valuePtr;

// display its value
cout << *valuePtr << endl;

return;

}



 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Yay Homework!

1. You didn't find any of the problems with the code, you just made it bigger. For example. you don't need cmath, iomanip, or cstdlib for anything, yet you added it anyways... Don't do that.

Im not sure how much to tell you other then I see 4 problems with this code. one Ill give you is that using the number 65 is bad, its much better to do 'A' + i rather then 65 as it is easier to read and more portable. (that would be the hardest to catch, and may not even have been one to catch.) The rest you would see pretty dang quickly if you compiled the code. Everything you added was useless for debuging and in fact added more bugs (Sorry if that sounds harsh, but its the sad truth :()

Well reading through the rest of the post, it looks like this is just all homework problems. So heres my advice.

Study up on arrays, know how they are delared, what they look like in memory, and how to avoid overflows.

Study up on pointers. You don't seem to have a grasp of what pointer are. This is a hard concept for most, so me just giving you the answer won't solve a thing for you.

Compile everything. Compilers have wonderful tools and messages built into them. if they throw you a warning, listen to it and fix it.

Learn what's in your includes. Don't just blindly add stuff to the top of the file and hope it compiles well.

Now, You will really need to do these homework assignments on your own if you want to learn C++. Programming is very much an interactive sort of learning, By me giving you an answer I only do a disservice to your ability to learn the stuff (and unfortunately there isn't much help I can give here without giving you the answer. So your kind of stuck in a situation where you'll just have to learn it for yourself).

Good luck, and don't get too discouraged.
 

deveraux

Senior member
Mar 21, 2004
284
0
71
Cogman pretty much summed it all up. There are a lot of syntax errors in the code you posted. Those are usually the easiest to fix with the compilers help. Compile often and get a feel for what the compiler is telling you or at least trying to tell you.

I will help you with another glaring error you are making with pointers though. This is a logical error that the compiler will not help you with. In the code you posted regarding the function swapPointers():

Think carefully what you are doing. Atm, you are doing it wrong. You want to re-assign a memory address, not the value within that memory address. So, right now, your variable is in fact, a memory address not in the other thread you posted in which the variable was an integer.

Think on this for a while to see if you can spot where you went wrong. This is important to help you get around pointers and will help you with the other code you posted.

Regarding the last piece of code you posted, did you write the getData() function or was it given to you in the course you are attending?

Either way, good luck.
 
Status
Not open for further replies.