Post fun questions here!

Hi, I'm still a beginner at programming and I do not have any classes or assignments and stuff. However, I would like to get more familiar with programming and be more efficient at it. Can you all post interesting questions for me to tackle under here?

Thank you!!

P.S. I'm still not confident in creating arrays and storing values into them especially if the number of elements in the array is determined by the user of the program.

P.S.S I'm currently learning about classes, constructors and stuff so feel free to post these questions as well.
Having problems with arrays eh? Let me give some array problems...(evil laugh).

Ok, here it is: (easy enough)
Write a program that will continually input up to 500 numbers, but will stop asking for numbers after the user enters a letter or word. After the user enters a letter, the program should ask users if they want to see the average or standard deviation, and display the user's choice. Average and standard deviation calculation should be a function.


Problem #2: (pretty tricky...)

Write a program that reads in an array of type int. You may assume that there are fewer
than 50 entries in the array. Your program determines how many entries are used. The output
is to be a two-column list. The first column is a list of the distinct array elements; the
second column is the count of the number of occurrences of each element. The list should
be sorted on entries in the first column, largest to smallest.
For the array values:
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output should be

N Count
4 2
3 3
2 2
1 4
-1 1
-12 4


Problem #3 (very tricky...)

Write a program that will allow two users to play tic-tac-toe. The program should ask for
moves alternately from player X and player O. The program displays the game positions as
follows:
1 2 3
4 5 6
7 8 9
The players enter their moves by entering the position number they wish to mark. After
each move, the program displays the changed board. A sample board configuration is as follows:
X X O
4 5 6
O 8 9


Problem #4: (very nearly impossible) <--- Challenge to everybody reading this. I'll be surprised if anyone can do that one!

The mathematician John Horton Conway invented the “Game of Life.” Though not a
“game” in any traditional sense, it provides interesting behavior that is specified with only a
few rules. This project asks you to write a program that allows you to specify an initial configuration.

The program follows the rules of Life (listed shortly) to show the continuing
behavior of the configuration.

LIFE is an organism that lives in a discrete, two-dimensional world. While this world is
actually unlimited, we don’t have that luxury, so we restrict the array to 80 characters wide
by 22 character positions high. If you have access to a larger screen, by all means use it.
This world is an array with each cell capable of holding one LIFE cell. Generations mark
the passing of time. Each generation brings births and deaths to the LIFE community. The
births and deaths follow this set of rules:

1. We define each cell to have eight neighbor cells. The neighbors of a cell are the cells
directly above, below, to the right, to the left, diagonally above to the right and left, and
diagonally below, to the right and left.

2. If an occupied cell has zero or one neighbor, it dies of loneliness. If an occupied cell has
more than three neighbors, it dies of overcrowding.

3. If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to
replace the empty cell.

4. Births and deaths are instantaneous and occur at the changes of generation. A cell dying
for whatever reason may help cause birth, but a newborn cell cannot resurrect a cell that is
dying, nor will a cell’s death prevent the death of another, say, by reducing the local population.

----------------------------*
Examples: *** becomes * then becomes *** again, and so on.
----------------------------*

(disregard all the -, they were supposed to be spaces, but it was not rendering it)

Notes: Some configurations grow from relatively small starting configurations. Others move
across the region. It is recommended that for text output you use a rectangular char array
with 80 columns and 22 rows to store the LIFE world’s successive generations. Use an * to
indicate a living cell and use a blank to indicate an empty (or dead) cell. If you have a screen
with more rows than that, by all means make use of the whole screen.

Suggestions: Look for stable configurations. That is, look for communities that repeat patterns
continually. The number of configurations in the repetition is called the period. There
are configurations that are fixed, that is, that continue without change. A possible project is
to find such configurations.

Hints: Define a void function named generation that takes the array we call world, an
80-column by 22-row array of type char, which contains the initial configuration. The
function scans the array and modifies the cells, marking the cells with births and deaths in
accord with the rules listed previously. This involves examining each cell in turn and either
killing the cell, letting it live, or, if the cell is empty, deciding whether a cell should be born.
There should be a function display that accepts the array world and displays the array on
the screen. Some sort of time delay is appropriate between calls to generation and display.
To do this, your program should generate and display the next generation when you
press Return. You are at liberty to automate this, but automation is not necessary for the
program.


All of these problems were taken from Absolute C++ by Walter Savitch. I do not own it.

Hope this helped,
VX

PS
I'll be very, very surprised if anyone can do #4.
Last edited on
Thanks for the qustions :D
I just completed question 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

void sd(int arg[], int n)
{
float avg = 0;
int num;
float stand = 0;
for(int y = 0; y < n; y++)
    {
    avg += arg[y];
    }
avg /= n;
for(int z = 0; z < n; z++)
    {
    num = arg[z] - avg;
    stand += pow(num, 2);
    }
stand /= n;
stand = pow(stand, 0.5);
cout << "Standard Deviation is " << stand << endl;
}

void average(int arg[], int n)
{
float avg = 0;
for(int y = 0; y < n; y++)
{
    avg += arg[y];
}
avg /= n;
cout << "Your average is " << avg << endl;
}

int main()
{
int item[500];
string stop;
string choice;
for(int x = 0; x < 500; x++)
{
    cin >> item[x];
    while(!cin)
    {
        cout << "Do you wish to stop? \n";
        cin.clear();
        cin.ignore(1, '\n');
        cin >> stop;
        if(stop=="y"||stop=="yes"||stop=="Yes"||stop=="YES"||stop=="Y")
        {
            cout << "Would you like the average or standard deviation of your numbers?\n";
            cin >> choice;
            if(choice=="average"||choice=="Average"||choice=="AVERAGE")
            {
            average(item, x);
            return 0;
            }
            else if(choice=="standard deviation"||choice=="Standard Deviation"||choice=="SD"||choice=="sd")
            {
            sd(item, x);
            return 0;
            }
            else{return 0;}
        }
        else if(stop=="n"||stop=="no"||stop=="No"||stop=="NO"||stop=="N")
        {
        cout << "Please enter an integer! \n";
        cin >> item[x];
        }
        else
        {
            while(stop!="y"&&stop!="yes"&&stop!="Yes"&&stop!="YES"&&stop!="Y"&&stop!="n"&&stop!="no"&&stop!="No"&&stop!="NO"&&stop!="N")
            {
            cout << "Please type Yes or No \n";
            cin >> stop;
            }
        }
    }
}

}


is it right? :D
Last edited on
Yep! Good job!
Question 2(Help check pls xD):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main()
{
int Array[6] = {4,3,2,1,-1,-12};
int times[6] = {0};
srand(time(0));
int NoT = rand();
NoT = NoT % 50 + 1;
int elements[NoT];
for(int x = 0; x < NoT; x++)
{
    int NoT1 = rand();
    NoT1 = NoT1 % 6;
    elements[x] = Array[NoT1];
    for (int y = 0; y < 6; y++)
    {
        if(elements[x] == Array[y])
        {
            times[y]++;
        }
    }

}
cout << "N ----- Count" << endl;
for(int z = 0; z < 6; z++)
{
    cout << Array[z] << " ------- " << times[z] << endl;
}
}
Question 3(Enjoyed the process of playing this one for some reason):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <string.h>

using namespace std;

bool checktie();
bool checkwin();
void changevalueX(int y);
void changevalueO(int y);
void displayBoard();
string ttt[9] = {"1","2","3","4","5","6","7","8","9"};

int main()
{
int answer;
while (checkwin() == false)
{
    displayBoard();
    cout << "\n Player X: \n";
    cin >> answer;
    cout << endl;
    answer -= 1;
    changevalueX(answer);
   if (checkwin() == false && checktie() == false)
    {
        displayBoard();
        cout << "\n Player O: \n";
        cin >> answer;
        cout << endl;
        answer -= 1;
        changevalueO(answer);
        if (checkwin() == true)
        {
            cout << "\n The winner is Player O!!!";
            return 0;
        }
    }
    else if (checkwin() == true)
        {
            cout << "\n The winner is Player X!!!";
            return 0;
        }
    else if (checktie() == true)
    {
        cout << "\n It's a tie!!!";
        return 0;
    }
}
}
void displayBoard()
{
    for (int x = 0; x < 3; x++)
    {
        cout << ttt[x] << " ";
    }
    cout << endl;
    for (int x = 3; x < 6; x++)
    {
        cout << ttt[x] << " ";
    }
    cout << endl;
    for (int x = 6; x < 9; x++)
    {
        cout << ttt[x] << " ";
    }
    cout << endl;
}

void changevalueX(int y)
{
    for (int x = 0; x < 9; x++)
    {
        if (x == y)
        {
            ttt[x] = "X";
        }
    }
}
void changevalueO(int y)
{
    for (int x = 0; x < 9; x++)
    {
        if (x == y)
        {
            ttt[x] = "O";
        }
    }
}
bool checkwin()
{
    if(ttt[0]==ttt[1]&&ttt[1]==ttt[2])
    {
        return true;
    }
    else if(ttt[3]==ttt[4]&&ttt[4]==ttt[5])
    {
        return true;
    }
    else if(ttt[6]==ttt[7]&&ttt[7]==ttt[8])
    {
        return true;
    }
    else if(ttt[0]==ttt[3]&&ttt[3]==ttt[6])
    {
        return true;
    }
    else if(ttt[1]==ttt[4]&&ttt[4]==ttt[7])
    {
        return true;
    }
    else if(ttt[2]==ttt[5]&&ttt[5]==ttt[8])
    {
        return true;
    }
    else if(ttt[2]==ttt[4]&&ttt[4]==ttt[6])
    {
        return true;
    }
    else if(ttt[0]==ttt[4]&&ttt[4]==ttt[8])
    {
        return true;
    }
    else{return false;}
}
bool checktie()
{
    int arg = 0;
    for(int x = 0; x < 9; x++)
    {
        if(ttt[x]!="1"&&ttt[x]!="2"&&ttt[x]!="3"&&ttt[x]!="4"&&ttt[x]!="5"&&ttt[x]!="6"&&ttt[x]!="7"&&ttt[x]!="8"&&ttt[x]!="9")
        {
            arg += 1;
        }
        else
        {
            return false;
        }
    }
    if(arg == 9)
    {
        return true;
    }
}
Well done, well done! I wish you luck on question 4...
closed account (L1vUM4Gy)
Thanks for the qustions :D
I just completed question 1

Your if statements are too long. You should use function convertStringToLower for easily checking. If the function does not exist, you can make one.

I wonder what would happen if an user enter...
Standard deviation


Or :
Sd


Or may be, if your program does not understand what the user says, ask the user to enter again instead of exiting the program.

Question 2(Help check pls xD)


1
2
3
int NoT = rand();
NoT = NoT % 50 + 1;
int elements[NoT];


Declaring an array like this is non-standard and other compilers may not support it. Maybe you should use a vector, or a dynamic array.

int Array[6] = {4,3,2,1,-1,-12};

This is not random. And that is not the purpose of the assignment.
Also, the data array should at least have 10 - 12 elements so that you can make decent result.

Conclusion : You have failed the question 2.

Question 3 :
#include <string.h>

Should be :
#include <string>

If the box already has X or O, you should prevent the player from choosing the box they have already placed.

1
2
3
4
bool checktie()
{
        return (ttt[0]!="1" && ttt[1]!="2" && ttt[2]!="3" && ttt[3]!="4" && ttt[4]!="5"&&ttt[5]!="6" && ttt[6]!="7" && ttt[7]!="8" && ttt[8]!="9");    
}


You don't need to check 9 times. Just a boolean statement is more than enough.

Problem #4: (very nearly impossible) <--- Challenge to everybody reading this. I'll be surprised if anyone can do that one!

It is very possible that this relates to Genetic Algorithm.
closed account (L1vUM4Gy)
This assignment requires vectors, but you could also use arrays but that is harder.

Write a program to enter a string containing random numbers and words. Try to extract every number in the string and store each number in a number vector and extract every word in the string and store each word in a string vector. After that, print out all elements in the number vector first, then the string vector.


Your vectors :
1
2
vector<int> number_array;
vector<string> string_array;


For example, if the user inputs :
dfd 557 sdj 113 356 msdn 77553 sdhs asas

The number vector contains : 557, 113, 356, 77553
The string vector contains : dfd, sdj, msdn, sdhs, asas

And the expected program output :
557
113
356
77553
dfd
sdj
msdn
sdhs
asas


Tip : Use std::stringstream to do the job.

It is a great practice. Good luck on this one.
Topic archived. No new replies allowed.