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;
}
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;
}
