Finding multidimensional array pattern for every odd j index - c++

Ok, so I'm trying to insert a space character for every odd number of j (including 0), the problem is that 0 counts as an even number (I want it to count as odd so a space character can be placed) and I'm struggling with how to figure out a solution other than playing with odd or even j. The goal is to make every element in the array have a letter and have a space character for the next index. This function is responsible for filling up the array.
void createBoard(char arr [DIM][DIM], int size){
//ASCII number for capital A
char x = 65;
for(int i = 0; i<size; i++){
for(int j = 0; j<size; j++){
if(j%2==0){
arr[i][j] = x++;
}
else{
arr[i][j] = 32;
}
cout << "Element at x[" << i << "][" << j << "]: ";
cout << arr[i][j] << endl;
}
}
}
This is function main.
const int DIM = 7;
int main()
{
char arr [DIM][DIM];
int bsize;
char answer;
do{
cout << "Please enter the size of the board [1-7]: ";
cin >> bsize;
if(!cin){
cout << endl << "Invalid entry";
break;
}
if (bsize<=DIM && bsize>=1){
createBoard(arr,bsize);
}
else{
cout << endl << "Invalid size";
cout << endl << "Do you want to try again [y-n]?: ";
cin >> answer;
}
// As long as the answer is 'y' (in upper or lower case), keep looping
}while(answer=='Y'||answer=='y');
return 0;
}

Related

Showing values in an array

I have been coding another programming challenge from a book. It is about asking a user to input n numbers inside an array. Every time the user inputs, the current numbers entered should show up. Numbers less than or equal to zero should not be accepted. I have managed to do the first condition. However, it even shows the "empty" slots. I have tMy code will explain it. Here:
#include <iostream>
using namespace std;
int main() {
int size = 0, input_num [100], i, j;
cout << "Enter an array size: ";
cin >> size;
if (size <= 0) {
while (size <= 0) {
cout << "Enter an array size again: ";
cin >> size;
}
}
cout << "\n\nYou may now enter " << size << " numbers ";
cout << "\n------ ------ ------ ------\n";
for (i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> input_num [i];
cout << "\nEntered Numbers: ";
for (j = 0; j < size; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";
}
}
In your output section, it should be j <= i, not j < size.
cout << "\nEntered Numbers: ";
for (j = 0; j <= i; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";

Is there a way to clear empty array in c++ , check code below for explanation

generated = (rand() % 11);
cout << "GUESS THE NUMBER 10 TIMES" << endl;
for (int i = 0; i < 10; i++) {
cin >> guesses[i];
if (generated == guesses[i]) {
cout << "CONGRATS ! YOU HAVE FOUND THE CORRECT NUMBER ! " << generated << endl;
restart();
break;
}
}
cout << "HERE ARE YOUR GUESSES:" << endl;
for (int k = 0; k < 10; k++) {
cout << guesses[k] << endl;
}
If the array isn't filled up, the output gives me the guessed values and then in the empty slots are random numbers. Is there a way to clear or delete the untouched slots?
Yes. Keep a count of how many inputs you take c and loop up to c instead of 10 in your second loop:
int c = 0;
cout << "GUESS THE NUMBER 10 TIMES" << endl;
for (int i = 0; i < 10; i++) {
cin >> guesses[i];
c++;
if (generated == guesses[i]) {
cout << "CONGRATS ! YOU HAVE FOUND THE CORRECT NUMBER ! " << generated << endl;
restart();
break;
}
}
cout << "HERE ARE YOUR GUESSES:" << endl;
for (int k = 0; k < c; k++)
cout << guesses[k] << endl;

about arrays in c++

i've been studying c++ for 3 months , and i studied the arrays , i wrote a program such that will take inputs from user , then the program will store these numbers in a special array , then the program will split them into two arrays , one for even numbers, the other one for odd numbers , my question is , when i tried to display them , there was something wrong happened , but i could not figure it out , can you help me please ?
int main () {
int even[5];
int odd[5];
int num;
cout << "enter 4 numbers!";
for(int i=0; i<4; i++) {
cin >> num;
if( num%2 == 0){
cout << "its an even number!";
even[i] += num;
}
else{
cout << "its an odd number!";
odd[i] += num;
}
}
cout << "The odd number/s is/are: ";
for( int u=0; u<4; u++){
cout << odd[u] << endl;
}
cout << endl;
cout << "The even number/s is/are: " << endl;
for(int z=0; z<4; z++){
cout << even[z] << endl;
}
}
Thank you for helping me!
From your question, it looks like you're trying to split a given integer array into two arrays even and odd. The problem here is the way in which you're allocating the values into the new arrays, You have a counter i which is responsible to put the values into even[i] and odd[i]. So you have a lot of broken sections even[0] might exist but the odd[1] might be the first odd value you obtain. You should have individual counters for storing these values. So the corrections to your code would look as follows
int main () {
int even[5];
int odd[5];
int num;
int evencount = 0;
int oddcount = 0;
cout << "enter 4 numbers!";
for(int i=0; i<4; i++) {
cin >> num;
if( num%2 == 0){
cout << "its an even number!";
even[evencount++] = num;
}
else{
cout << "its an odd number!";
odd[oddcount++] = num;
}
}
cout << "The odd number/s is/are: ";
for( int u=0; u < oddcount; u++){
cout << odd[u] << endl;
}
cout << endl;
cout << "The even number/s is/are: " << endl;
for(int z=0; z<evencount; z++){
cout << even[z] << endl;
}
}
I have a slightly different approach:
#include <iostream>
using namespace std;
int main () {
int array[4]; // array size needs to be 4 only and not 5
/*int num;*/ // not required
bool is_odd[4] = {false, false, false, false};
cout << "enter 4 numbers!\n";
for(int i=0; i<4; i++) {
cin >> array[i];
if( array[i]%2 == 0){
cout << "its an even number!\n";
}
else{
cout << "its an odd number!\n";
is_odd[i] = true;
}
}
cout << "The odd number/s is/are:\n";
for( int u=0; u < 4; u++){
if (is_odd[u] == true)
cout << array[u] << endl;
}
cout << endl;
cout << "The even number/s is/are:\n" << endl;
for(int u=0; u < 4; u++) {
if (is_odd[u] == false)
cout << array[u] << endl;
}
}
Check the answer here: check-answer
You access values of the array that are declared but not initialized. So there are any values in it. like 6.49e154. Always initialize when declaring!

error string subscript out of range c++

i have a guessed name game. and i have a problem with this function. in the nested loop if the user entered a wrong letter it gives me this error:
line: 1440
expression: string subscript out of range
this is the function:
void Play(int selection, int FArraySize, int MArraySize,string Female[], string Male[])//Receive the selected name category array and its size and implements the game logic as shown in the sample run. Uses function GetRandomName(…).
{
int MAX_TRIES = 4;
int m = 0;
int x=0;
int j=0;
ofstream ofFile;
ifstream InFile;
int num_of_wrong_guesses=0;
char letter;
string GuessedName;
GuessedName = GetRandomName(selection, FArraySize, MArraySize, Female, Male);
cout << "Guess the following name:" << endl;
for(int y = 0; y < GuessedName.length(); y++){
cout << "?";
j++;
}
cout << "\nEnter a guess letter? or * to enter the entire name" << endl;
cin >> letter;
int i =0;
for ( int count = 0 ; count <= MAX_TRIES ; count++)
{
while (i <= j)
{
if (letter == GuessedName[i])
{
i = m;
cout << letter << " exists in the name";
cout << "\nyou have " << MAX_TRIES << " remaining guess attemps... "<< endl;
break;
}
if (i == j)
{
cout <<"Sorry! " << letter << " dose not exist in the name";
cout << "\nyou have " << MAX_TRIES-- << " remaining guess attemps... ";
break;
}
i++;
}
cout << "\nGuess the following name:" << endl;
for(int y = 0; y < GuessedName.length(); y++){
cout << "?";
j++;
}
cin >> letter;
}
return;
}
hope you can help me.
while (i <= j) should be while (i < j).
Otherwise GuessedName[i] will try to access out-of-bounds when i == j. j - 1 is the index of the last letter.

Pointer with cin implementation issue

I'm a new bie to CPP. I'm trying to use pointer and cin combination which is giving strange result.
int *array;
int numOfElem = 0;
cout << "\nEnter number of elements in array : ";
cin >> numOfElem;
array = new (nothrow)int[numOfElem];
if(array != 0)
{
for(int index = 0; index < numOfElem; index++)
{
cout << "\nEnter " << index << " value";
cin >> *array++;
}
cout << "\n values are : " ;
for(int index = 0; index < numOfElem; index++)
{
cout << *(array+index) << ",";
}
}else
{
cout << "Memory cant be allocated :(";
}
The out put is
What the problem with my code ?
Regards,
Sha
The array++ inside the loop increments the pointer, so by the time you're done with the first loop, array will point outside the originally allocated array.
Just do
cin >> *(array+index);
or simply
cin >> array[index];
You are advancing the pointer, array, in the first loop:
for(int index = 0; index < numOfElem; index++)
{
cout << "\nEnter " << index << " value";
cin >> *array++;
}
And then you pretend you are using the original, unmodified pointer in the second loop:
cout << "\n values are : " ;
for(int index = 0; index < numOfElem; index++)
{
cout << *(array+index) << ",";
}