Print amount of repeated numbers in array c++ - c++

I was wondering how to print the amount of repeated numbers from a randomly generated array with an array size of 10, and numbers from 1 - 10.
Example:
Array1: 1 7 6 5 6 7 8 10 9 8
Number of Patterns: 3
(Because it consists of; 2 six, 2 seven, 2 eights)
So far for my code I have done this
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
#include "Source.h"
#include <algorithm>
using namespace std;
void main()
{
//START OF PROGRAM CODE\\
//Declaritions\\
const int ArraySize = 10;
int arrayMain[ArraySize];
int array1[ArraySize];
int i = 0;
int j = 0;
int k = 0;
//End of Declairations\\
//Store Random Number in Array\\
srand((unsigned)time(0));
for (i = 0; i < 10; i++)
{
arrayMain[i] = (rand() % 10) + 1;
array1[i] = arrayMain[i]; //Copy mainarray to array1
}
for (j = 0; j != ArraySize; j++)
{
sort(array1, array1 + ArraySize); //Sort the array
}
//End of Store Random Number in Array\\
//Program Output\\
cout << "ArrayMain: " << arrayMain[0] << " " << arrayMain[1] << " " << arrayMain[2] << " " << arrayMain[3] << " " << arrayMain[4] << " " << arrayMain[5] << " " << arrayMain[6] << " " << arrayMain[7] << " " << arrayMain[8] << " " << arrayMain[9] << " " << endl;
cout << "Array1: " << array1[0] << " " << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << endl;
//cout << "Number of Patterns: " << <DATA TO INPUT> << endl;
//END OF PROGRAM CODE\\
}

There's a very simple pattern. You could use an array (I'll be using a vector) of a size equal to the range of possible random numbers
//creates a vector ArraySize big with all elements initialized to 0
std::vector<int> results(ArraySize, 0);
Then, go through your loop and use the random numbers as indexes and increment the values
for(int i = 0; i < 10; i++)
results[(rand() % 10)]++;
Finally, to count how many patterns there are
std::cout << "Number of patterns: ";
std::cout << std::count_if(results.begin(), results.end(), [](int i){return i > 1;});
std::cout << std::endl;

Related

how to find sum of cells surrounding a user input cell in 2d array?

this is what I have so far with the srand(0) omitted for ease of calculation the values are close except for the outer limits. the formatting for the output is all correct but the sum value for the surrounding cells is usually in the millions if the chosen cell is on column or row 9.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int matrix[9][9];
int row_n, col_n, sum, surround_sum, i, j;
cout << "\t columns" << endl;
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
for (int i = 0; i < 9; i++)
{
cout << "row " << i + 1 << " ";
for (int j = 0; j < 9; ++j)
{
matrix[i][j] = rand() % 10;
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "what array cell would you like to see? (press enter after each entry)" << endl;
cout << "row = ";
cin >> row_n;
cout << "column = ";
cin >> col_n;
cout << "the number " << matrix[row_n - 1][col_n - 1] << " is in cell " << row_n << "," << col_n << endl;
for (int i=(row_n-2);i<(row_n+1);i++){
for(int j=(col_n-2);j<(col_n+1);j++){
if (i>9||i<0)
i=0;
if (j>9||j<0)
j=0;
sum+=matrix[i][j];
surround_sum= sum-matrix[row_n-1][col_n-1]+2;
}}
cout << "the sum of the cells surrounding cell " << row_n << "," << col_n << " is " << surround_sum;
return 0;
}

Using pointers to duplicate and grow an existing array

I am failing to reach expected output when testing my 'grow'and 'subArray' functions. I've tried dereferencing back and forth in the function and also in main(). I'm wondering if there's something wrong with my memory allocation that is causing the lapse. I am extremely stuck and was hoping someone could potentially see something that I am missing, thanks.
#include <iostream>
#include <iomanip>
using namespace std;
bool isSorted(int *arr, int size){
for(int index = 0; index < size - 1; index ++){
if(*(arr + index) > *(arr + index + 1)){
return false;
}
}
return true;
}
double chain (int totalInches, int *feet, int *inches){
*feet = totalInches/12;
*inches = totalInches%12;
return *(feet)*3.49 + *(inches)*.30;
}
int *grow (int *arr, int size){
int *newArray;
newArray = new int[size*2]; //alocate new array
for(int i = 0; i < size*2; i+=2){
*(newArray + i) = *(arr+i);
*(newArray + i + 1) = *(arr+i);
}
return newArray;
}
int *duplicateArray (int *array, int size) {
int *newArray;
if (size <= 0)
return NULL;
newArray = new int [size]; //allocate new array
for (int index = 0; index < size; index++){
newArray[index] = array[index]; //copy to new array
}
return newArray;
}
int *subArray( int *array, int start, int length){
int *result = duplicateArray(array,5);
return result;
}
void showArray( int *arr, int size){
for(int i = 0; i < size; i ++)
{
cout << *(arr + i) << " ";
}
}
int main(){
int size = 8;
int testArray[] = {1,2,3,4,5,6,7,8};
cout << "testing isSorted: " << endl;
cout << "test data array 1: ";
showArray(testArray, size);
cout << endl;
cout << "Expected result: true" << endl;
cout << "Actual result: " << boolalpha << isSorted(testArray, size);
cout << endl;
int testArray2[]= {8,7,6,5,4,3,2,1};
cout << "test data array 2: ";
showArray(testArray2, size);
cout << endl;
cout << "Expected result: false" << endl;
cout << "Actual result: " << boolalpha << isSorted(testArray2, size);
cout << endl << endl << endl;
int chainTest = 53;
cout << "Checking chain for 53 inches: " << endl;
cout << "Expected result: 15.46 " << " " << "feet: 4 " <<
" " << "inches: 5"<< endl;
int in;
int ft;
cout << "Actual results : " << chain(chainTest,&ft,&in);
cout << " " << "feet: " << ft << " " << "inches: " << in << endl;
cout << endl << endl;
cout << "testing grow: " << endl;
cout << "test data 1: ";
showArray(testArray, size);
cout << endl;
cout << "Expected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 " << endl;
cout << "Actual results: " << *(grow(testArray, size));
cout << endl << endl;
cout << "testing subArray:" << endl;
cout << "test data: ";
showArray(testArray, size);
cout << endl;
int start = 5;
int length = 3;
cout << "start: " << start << " " << "length: " << length << endl;
cout << "Expected result: " << "6 7 8" << endl;
cout << "Actual result: " << *(subArray(testArray, start, length));
cout << endl;
return 0;
}
Output:
As you notice, the loop is terminating after one traversal. The grow function is intended to duplicate and expand. In other words, it's supposed to make a copy of itself and append as it traverses. Any ideas as to why I am getting hung on the first element of the array?
You are actually doubling the array but only the first element is being printed because you are dereferencing an int* . To print all the elements, write a loop and print all the elements.
Also there is so much memory leak here. Please free memory after you are done using it. You are read about the delete[] operator.
Your loop going two at a time is good but it prevents you from selecting every element in the original array causing you to skip the even numbers. check your for loop and consider using two counters or if you want to modify your for loop to
for(int i = 0; i < size*2; i+=2){
*(newArray + i) = *(arr+i/2);
*(newArray + i + 1) = *(arr+i/2);
}
to ensure every element is reached
also as stated in the comments, use the showArray method you implemented
showArray(grow(testArray, size),size*2);

Dice game that requires us to number our turns along with adding * before every dice roll

visit http://voyager.deanza.edu/~bentley/ass5.html
My goal is trying to match the sample output that is on the link. The only obstacles that I can not seem to overcome is how to add what turn you are on and also "*" before each line dice roll.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int roll();
int turn();
int main ()
{
srand(time(0));
int gameTotal = 0;
while (gameTotal < 100)
{
gameTotal += turn();
cout << "Your total is now " << gameTotal << endl << endl;;
}
}
int turn()
{
int turnTotal = 0;
int temp;
for (int i = 0; i < 3; i++)
{
temp = roll();
if (temp == 7) break;
turnTotal += temp;
}
cout << "You scored " << turnTotal << " points for this turn" << endl;
return turnTotal;
}
int roll()
{
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int sum = die1 + die2;
cout << "You rolled a " << die1 << " and " << die2 << ". " << "That's " << sum << endl;
return sum;
}
Just add a variable to store the number of turns played and update it at every turn. adding the * is even simplier:
int main ()
{
srand(time(0));
int gameTotal = 0;
int turns = 0;
while (gameTotal < 100)
{
// update number of turns and output it
++turns;
cout << "This is your turn #" << turns << endl;
gameTotal += turn();
cout << "*** Your total is now " << gameTotal << endl << endl;
// ^^^ easy
}
}
Similar changes in the other functions:
cout << "** You scored " << turnTotal << " points for this turn" << endl;
// ^^
and
cout << "* You rolled a " << die1 << " and " << die2 << ". " << "That's " << sum << endl;
// ^

C++ Array Error

I created a random, and its size is created randomly. Then I assign random values to this array. Finally, I want to write odd values and even values into different arrays. But the last two for loops display wrong values for evenArray and oddArray.
Where is the error? Please help me identify the error.
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
int arraySize = rand() % 10 + 4;
cout << "Array Size : " << arraySize << endl;
int myArray[arraySize];
int oddIndex = 0;
int evenIndex = 0;
int oddArray[oddIndex];
int evenArray[evenIndex];
for( int m = 0 ; m < arraySize ; m++)
{
myArray[m] = rand() % 100 + 90 ;
cout << m << "th value is : " << myArray[m] << endl;
}
for( int i = 0; i < arraySize ; i++)
{
if( myArray[i] % 2 == 0)
{
evenArray[evenIndex] = myArray[i];
cout << "EVEN ARRAY " << evenIndex << "th element is " << evenArray[evenIndex] << endl;
evenIndex++;
}
else
{
oddArray[oddIndex] = myArray[i];
cout << "ODD ARRAY " << oddIndex << "th element is " << oddArray[oddIndex] << endl;
oddIndex++;
}
}
cout << "The total number of even array elements : " << evenIndex << endl;
cout << "The total number of odd array elements : " << oddIndex << endl;
cout << "/////////////////////////////////////////\n";
cout << "EVEN VALUES" << endl;
for( int i = 0 ; i < evenIndex ; i++ )
{
cout << i << "th even value is: " << evenArray[i] << endl;
}
cout << "/////////////////////////////////////////\n";
cout << "ODD VALUES" << endl;
for( int p = 0 ; p < oddIndex ; p++ )
{
cout << p << "th odd value is : " << oddArray[p] << endl;
}
}
int oddIndex = 0;
int evenIndex = 0;
int oddArray[oddIndex];
int evenArray[evenIndex];
As you can see you are initializing oddArray and evenArray with 0 size.What you can do is declare both arrays of size arraySize,or better you can use vectors
You use wrong initialization of arrays length
int oddArray[oddIndex];
int evenArray[evenIndex];
In this lines you create two arrays of zero length. Further use to these arrays will lead to undefined behaviors.
If you need an array with dynamically changing length you should use std::vector.

trying to order 3 values from least to greatest C++

I have a program that takes numbers that a person enters and sums it.
This happens 3 times, so I have 3 totals. The problem I am having is that I need to order them from greatest to least no matter what the sums come out to be.(this isnt the full code assume the sums are calculated and are declared)
#include <iostream>
#include <string>
using namespace std;
string firstName1, lastName1; // input and output for the users names
string firstName2, lastName2;
string firstName3, lastName3;
// from greatest to least
if ( sum > sum_2 > sum_3 )
{
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sum << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sum_2 << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sum_3 << ".00" << endl;
}
In c++, the syntax sum > sum_2 > sum_3 won't evaluate as you're assuming. It's equivalent to (sum > sum_2) > sum_3.
In the case where sum is greater than sum_2, sum > sum_2 will evaluate to true. Then, this boolean value will be converted to an integer, 1 and compared with sum_3.
To do what you're trying to accomplish try:
if (sum > sum_2 && sum_2 > sum_3)
Use a helper swap function:
void swap( int *a, int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
And bubble sort it:
int sums[3] = { sum, sum_2, sum_3 };
for ( int i = 0; i < 3; ++i )
for ( int j = 0; j < i; ++j )
if ( sums[j] < sums[i] )
swap( &sums[j], &sums[i] );
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sums[0] << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sums[1] << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sums[2] << ".00" << endl;