Using the string class?

I have to do this lab for one of my classes. I get to step 4 and do not see any 0's printed. Also, the only numbers that are opposite in my reverse array are the first and second ones. Please help someone? thanks!


Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages. Run the program to see what it prints. You should see 10 random looking numbers between 0 and 99. These are the 10 values you have stored in the data array.

Step 2: You should have noticed that we used the constant DATA_SIZE in three places in the program, when we declared the data array and in both of the for loops that process this array. Edit your program and change the value of DATA_SIZE to 20, and recompile and run the program. Now you should see 20 random numbers. By using constants, it is much easier to increase/decrease the size of arrays and the loops that process these values.

Step 3: Edit your program and copy/paste the "print array" loop so it is just before the "initialize array" loop. Now compile and run the program. What do you see? These are the values that C++ has stored in the array when you declared it. Now you can see that it is very important to initialize variables before you use them.

Step 4: Edit your program again, and change the "int data[DATA_SIZE];" declaration to "int data[DATA_SIZE] = {0};". This will initialize all of your array values to zero. Compile and run your program to verify this. You should now see 20 zeros printed on one line, and 20 random values on the next line.

Step 5: Arrays in C++ always store their values in locations [0..size-1]. If you attempt to read the array beyond these bounds the program will get junk values or it may crash. To verify this, edit your program and change your two "print array" loops so they go from [-10..size+10]. If your program does not crash, you should see some very strange numbers printed out. We should never do this on purpose, so change your program back to the original loops to print values [0..size-1].

Step 6: One of the big advantages of arrays is that we can rearrange the data in the array for different purposes. For example, we can reverse the order of the data in the array by swapping the first value with the last value, the second value with the second last value, etc. This can be accomplished with a for loop that swaps the values of data[index] with data[size-1-index]. Copy/paste the following loop into your program under the "reverse array" comment. Then compile and run the program to see what happens.

// Reverse array
for (int index = 0; index < DATA_SIZE; index++)
{
data[index] = data[DATA_SIZE-1-index];
data[DATA_SIZE-1-index] = data[index];
}

// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;
Step 7: There is a logic error in the reverse loop above. We can not swap the values of two variables A and B with two statements A = B; B = A; Once we have executed the first statement, the original value of A is lost. To solve this, we need a temporary variable T and the following: T = A; A = B; B = T; Use this logic to correct your reverse array loop and compile and run your program. What do you see?

Step 8: There is a second logic error in the reverse loop we gave you. As you noticed above, the reversed data ended up looking like the original data. This is because we ended up swapping each pair of array locations two times. Why? When index=0 we swap the first and last locations. Then when index=size-1 we swap the first and last locations again. To fix this, you simply need to stop the for loop when "index < DATA_SIZE/2". Make this change to your program and recompile and run. Now you should see the data in reverse order. Congratulations on a job well done.

Step 9: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.


This is the code that I have so far:

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

int main()
{
// Declare array of integers
const int DATA_SIZE = 20;
int data[DATA_SIZE] = {0};
int T;

// Initialize array
for (int index = 0; index < DATA_SIZE; index++)
data[index] = random() % 100;


// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;


// Reverse array
for (int index = 0; index < DATA_SIZE; index++)
{

T = data[index];
data[index] = data[DATA_SIZE-1-index];
data[DATA_SIZE-1-index] = T;


if (index < DATA_SIZE/2)
break;

}


// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;



return 0 ;
}


and the original code given:


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

int main()
{
// Declare array of integers
const int DATA_SIZE = 10;
int data[DATA_SIZE];

// Initialize array
for (int index = 0; index < DATA_SIZE; index++)
data[index] = random() % 100;

// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;

// Reverse array

return 0 ;
}
Last edited on
I don't see that this part of step 3 was done.

Step 3: Edit your program and copy/paste the "print array" loop so it is just before the "initialize array" loop. Now compile and run the program. What do you see? These are the values that C++ has stored in the array when you declared it. Now you can see that it is very important to initialize variables before you use them.
I changed it back to where the "initialize array" is before the "print array" because it says at the bottom that it is important to initialize variables before you use them. Should the print array still be first?
Yes if for the purpose of this exercise you want to see that the array is initialized with 0's here...

int data[DATA_SIZE] = {0};

...before you fill the array with random values here.

1
2
for (int index = 0; index < DATA_SIZE; index++)
data[index] = random() % 100;
Okay here is what I have now after step 3:
However, I still only see a line of 0's after i compile and run it. What am I doing wrong?



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

int main()
{
// Declare array of integers
const int DATA_SIZE = 20;
int data[DATA_SIZE] = {0};

// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;

// Initialize array
for (int index = 0; index < DATA_SIZE; index++)
data[index] = random() % 100;



// Reverse array

return 0 ;
}
You'll want multiple print statements if you want to see the output a different steps along the way through the program.
Topic archived. No new replies allowed.