Last week I had to make a dice rolling program that uses a one demensional array that stores and displays the number of times each sum of two dice appear. I completed this code with little effort. But this week we have to modify the code so that instead of getting stored into an array the number of times each sum appears gets stored into a vector. I am completely lost on how to modify my code to do this. Any hints that can be given to help me out with this will be greatly appreciated. Here is my code for the array program:

#include "home7.17.h"   

int main()  
{

	int sum[13] = {};  


	int sumOfDice = START_VAL;   

	

	
	for ( int roll = START_ROLL; roll <= TIMES_TO_ROLL; roll++ ){   

		
		sumOfDice = rollDice();   


		switch ( sumOfDice ){   


			case 2:   
				
				++sum[2];   

				break;  


			case 3:   

				++sum[3];   

				break;   


			case 4:   
				
				++sum[4];   

				break;   


			case 5:   
				
				++sum[5];   

				break;  


			case 6:   
				
				++sum[6];  

				break;   


			case 7:  
				
				++sum[7];   

				break;  


			case 8:  
				
				++sum[8];   

				break;   


			case 9:   
				
				++sum[9];  

				break;  


			case 10:   
				
				++sum[10];  

				break;   


			case 11: 
				
				++sum[11];  

				break;   


			case 12:  
				
				++sum[12];

				break;  


			default:   

				cout << "If this is seen error occurred.";   

		}   

	} 

	
	cout << "Sum" << setw( 20 ) << "\tNumber of Times" << endl;  

	cout << "  2" << setw( 20 ) << sum[2]   

		<< "\n  3" << setw( 20 ) << sum[3] 

		<< "\n  4" << setw( 20 ) << sum[4]   

		<< "\n  5" << setw( 20 ) << sum[5] 

		<< "\n  6" << setw( 20 ) << sum[6]  

		<< "\n  7" << setw( 20 ) << sum[7]  

		<< "\n  8" << setw( 20 ) << sum[8]   

		<< "\n  9" << setw( 20 ) << sum[9]  

		<< "\n  10" << setw( 19 ) << sum[10]  

		<< "\n  11" << setw( 19 ) << sum[11]

		<< "\n  12" << setw( 19 ) << sum[12] << endl << endl;

	


		return SUCCESS;

}

I don't think you'd have to change much at all to change this into a vector. In fact you may only have to change one line. Change this:

int sum[13] = {};

to this:

vector <int> sum (13, 0);

This creates a vector of 13 integers and initializes all of those integers to 0. Then just add this to the top:

#include <vector>

Wow that works perfectly, and was much simpler then anything i was trying to do. Thank you so much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.