Making sure a user's input matches characters generated by my program - c

This is for Homework
I'm programming a simplified game of scrabble where I have my program randomly generate characters then the user would try and create a word from those generated characters, then get a score afterwards. The issue I'm having is making sure the user is actually using the characters provided. I have no clue on how to approach this problem. I don't need any code but hints would be appreciated or even links for a point to start at. Thanks for any help!
EDIT - About half my program [The part that creates the letter set]
void generate_letter_set(int letter_set[] , int size_let , int num_let)
{
int arr[N];
const char let[] =
{'K','J','X','Q','Z','B','C','M','P','F','H','V','W','Y','G','L','S','U','D','N','R','T','O','A','I','E'};
const int freq[] =
{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 6, 6, 6, 8, 9, 9, 12 };
int score[] =
{ 5, 8, 8, 10, 10, 3, 3, 3, 3, 4, 4, 4, 4, 4, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
int index = 0;
for(int i = 0 ; i < 26 ; i++) {
for(int f = 0 ; f < freq[i]; f++) {
arr[index++] = let[i]; //All the 96 letters are stored in let[i]
//printf("%c " , let[i]); // Created the letter bank for all the letters
}
} int letter;
printf("Your letters are: ");
for(int l = 0; l < 7; l++){
letter = rand() % 97;
printf("%c ", arr[letter]);
}
}

There are a lot of different ways to search an array for certain characters. The basis of what you need is a very simple search function.
One simple solution would be to use two nested for loops. Assuming let[] is your 'haystack' to check and word is your user input:
// Check each letter of word[]...
for (int ii = 0; ii <= lengthOfUserInput; ii++)
{
char characterToValidate = word[ii];
// ... for not existing in let[]
for (int jj = 0; jj <= lengthOfStringOfValues; jj++)
{
if (characterToValidate != let[jj])
}
}

Related

How can I reduce the execution time in this code?

Problem
Consider the sequence D of the last decimal digits of the first N Fibonacci numbers, i.e. D = (F0%10,F1%10,…,FN−1%10).
Now, you should perform the following process:
Let D=(D1,D2,…,Dl)
If l=1, the process ends.
Create a new sequence
E=(D2,D4,…,D2⌊l/2⌋)
In other words, E is the sequence created by removing all odd-indexed elements from D
Change D to E
When this process terminates, the sequence D
contains only one number. You have to find this number.
Input
The first line of the input contains a single integer T
denoting the number of test cases.
The description of T test cases follows.
The first and only line of each test case contains a single integer N
Output
For each test case, print a single line containing one integer ― the last remaining number.
Code
#include <stdio.h>
#include <stdlib.h>
int test(int *arr, int n);
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int *arr;
arr = (int *)malloc((n + 1) * sizeof(int));
arr[1] = 0;
arr[2] = 1;
for (int i = 3; i <= n; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
/*
for(int k=1;k<=n;k++){
printf("%d ",arr[k] );
}
printf("\n");
*/
printf("%d\n", (test(arr, n)) % 10);
}
}
int test(int *arr, int n) {
if (n == 1) {
return arr[1];
} else {
for (int i = 1; i <= (n / 2); i++) {
arr[i] = arr[2 * i];
}
return test(arr, n / 2);
}
}
Using the algorithm from https://math.stackexchange.com/questions/681674/recursively-deleting-every-second-element-in-a-list,
Find the largest integer A, such that 2^A < N.
Find Fibonnaci(2^A - 1) % 10
Adding to Bill Lynch's answer, which is itself based on this other answer by happymath:
You will always end up getting 2n − 1 where n is maximum integer such that 2n < K
I'd like to point out another useful mathematical property.
In number theory, the nth Pisano period, written π(n), is the period with which the sequence of Fibonacci numbers taken modulo n repeats.
(https://en.wikipedia.org/wiki/Pisano_period)
Here we need to consider the case where n = 10, π(10) = 60 and the last decimal digits correspond to the OEIS sequence A003893:
0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1
So that there's no need to calculate the actual Fibonacci number, nor to generate all the sequence up to N.

Every possible path from 2D array? (Cross Product)

Let's suppose I have this array
int diml = 3;
int dims = 3;
int time [diml][dims] ={
(10, 3, 5),
( 4, 7, 2),
( 2, 8, 1)
};
How can I get every combination like:
(10, 3, 5)
(10, 3, 2)
(10, 3, 1)
(10, 7, 5)
(10, 7, 2)
(10, 7, 1)
...
(2, 8, 5)
(2, 8, 2)
(2, 8, 1)
*Is this possible without saving all the combinations in a new array, but just a 1D local array that can store the current combination on every cycle?
*I'd prefer cycles over recursion. And at the end of each cycle I need the pattern (like 10, 3, 2) so I can elaborate it.
*The dimensions of the 2D array are MxN (3x3 is just an example).
*A solution with binary trees is accepted (but I want to save the indexes too).
I should do this in C. I have found similar solutions in StackOverflow but they work by column and they save the data in a 2D array, but that's not what I need.
Thanks in advance! (:
For this example codes, The first one was built so it would be easier to understand example 2. Example 1 was built for only 3x3 matrixes. Example 2 was built so that it can accommodate a matrix with 8 columns at maximum. I didn't use malloc or return an array. It will print back all the possible combinations for you. It doesn't deal with returning the data but it wouldn't be hard to incorporate that into the code.
For the method of calculation all the possible combination, I would use a 3x3 matrix as an example.
In a 3x3 matrix, there are 3 rows and 3 columns. I treated each column as a set of number that I can pick and the rows as the possible of numbers that I can pick from. So in that example, I can pick 012 for my first, second, and third set of number.
So to get all the possible combinations, I have 3 arrays, [0] [1] [2]. They all start at 0. I first save the possible combination of 0 0 0. Then I increase array 2 by 1. Then I would get 0 0 1. I then save that combination. I will keep on doing that and one the [2] array == 2. I turn that to 0 and add a 1 to the array to the left of it. So it become 0 1 0. When I reach a loop where my values of my arrays are 0 2 2, the loop after that, I will get 1 0 0. I will keep on doing that until all the value turn to zero then I am done.
For how the data is store, I store them continually in an array. To read back the value properly. Say for example in a 2x5 matrix. Each combination will have 5 numbers. Thus, the first combination is the first five indexes, the next combination, is the next five numbers after that one.
To calculate how much array length you would need before calculating the combinations, you can just base the calculation on rows and columns. Think of this like the lottery. If there are 3 columns, it like you can pick 3 numbers. Each column have 3 rows, so that mean for each time you pick a number there are 3 possible numbers to pick from. For you to hit a jackpot the chances are 1:3 x 1:3 x 1:3 or 1:27 because there are 27 possibilities if picking 3 numbers like this (123 123 123) and matching them in the correct order. Thus, for a 3x3 matrix, it is 3x3x3, 4x4 = 4x4x4x4, 1x3 = 1, 3x1 = 3, 2x5 = 2x2x2x2x2 = 32.
Thus, the amount of possible combinations is "the amount of rows" to the power of "the amount of columns".
The size is the amount of possible combinations multiply by the amount of numbers per combination. Of which would be "possibilities multiply column count = array size needed.
Example 1:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int m1 = 3;
const int m2 = 3;
int matrix[m1][m2] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};
printMatrixCombo(m1, m2, matrix);
return 0;
}
// Only use this for a 3x3
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int output[81] = {0};
for (int group1 = 0; group1 < 3; group1++){
for (int group2 = 0; group2 < 3; group2++ ){
for (int group3 = 0; group3 < 3; group3++ ){
output[oi++] = matrix[group1][0];
output[oi++] = matrix[group2][1];
output[oi++] = matrix[group3][2];
}
}
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}
Example 2:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int row = 4;
const int col = 4;
/*// 3x3
int matrix[row][col] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};//*/
// 4 x 4
int matrix[row][col] = {
{10, 3, 5, 7},
{4, 7, 2, 3},
{2, 8, 1, 9},
{9, 4, 8, 11}
};//*/
/*// 5 x 5
int matrix[row][col] = {
{10, 3, 5, 7, 25},
{4, 7, 2, 87, 42},
{2, 8, 1, 85, 39},
{9, 4, 8, 94, 57},
{10, 3, 5, 7, 93},
};//*/
/*// 2 x 2
int matrix[row][col] = {
{10, 3},
{4, 7},
};//*/
/*// 1 x 1
int matrix[row][col] = {
{10},
};//*/
/* // 3 x 1
int matrix[row][col] = {
{10},
{4},
{1}
}; //*/
/*// 1 x 3
int matrix[row][col] = {
{10, 4, 1},
};// */
printMatrixCombo(row, col, matrix);
return 0;
}
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int allZ = 0;
// This is the maximum for a 5x5
// Change to fit usage case
int output[15625] = {0};
int colCount[8] = {0};
int lastCol = col - 1;
int lastRow = row - 1;
while (1){
for ( int i = 0; i < col; i++ )
output[oi++] = matrix[colCount[i]][i];
if ( colCount[lastCol] == lastRow ){
colCount[lastCol] = 0;
for (int i = lastCol - 1; i > -1; i--){
if ( colCount[i] == lastRow ){
colCount[i] = 0;
} else {
colCount[i]++;
break;
}
}
} else {
colCount[lastCol]++;
}
allZ = 1;
for ( int i = 0; i < col; i++ ){
if ( colCount[i] != 0 ){
allZ = 0;
break;
}
}
if (allZ == 1) break;
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
printf("Array's length(indexes) is %d\n", oi );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}

C - delete element from array and reorganise

Given this array:
int a[] = {5, 8, 5, 6, 9, 5};
Would it be possible to remove all ints which equals 5 and move the rest the front of the array?
So that after the removal the array would look like this:
int a[] = {8, 6, 9, 0, 0, 0}
I don't know if by removing a element it becomes a 0 or a NULL?
Thanks!
You can do it with two iterations over the array, first iteration two to turn the element you want to delete, second iteration to separate zeros from non-zeros.
int a[] = {5, 8, 5, 6, 9, 5};
int n = 6;
for(int i = 0 ; i < n ; i++ ) {
if(a[i] == 5 ) {
a[i] = 0;
}
}
int* zero = a;
int* nonZero = a;
int j = 0;
while(j < n) {
while(*zero != 0) {
zero++;
}
while(*nonZero == 0) {
nonZero++;
j++;
}
if(zero < nonZero) {
*zero = *nonZero;
*nonZero = 0;
}
j++;
}
Your array is statically allocated, so always has the same size and deleted elements have the 0 value (according how you define the deleted values).
This link can help you and explains about how to delete element from array.
It is been awhile that i have programmed in C but it is posibble.
This is just a pseudo code, but you just need to change it to way of C programming.
int a[] = {5, 8, 5, 6, 9, 5};
int b[] = {5, 8, 5, 6, 9, 5}; // copy of array a to hold temp
for(int i = 0; i < Size of array; i++ ){
for(int j = i; j < Size of array; j++ ){
if(b[j] != 5){
a[i] = b[j];
a[j] = b[i];
break;
}
}
}
It will be like (▼: Target to swap, F: Finished, X: Not a targe to swap, N: Not processed):
▼, ▼, N, N, N, N
5, 8, 5, 6, 9, 5
F, ▼, X, ▼, N, N
8, 5, 5, 6, 9, 5
F, F, ▼, X, ▼, N
8, 6, 5, 5, 9, 5
Result:
8, 6, 9, 5, 5, 5
And remove 5s, it is quite different depends what you mean. If you do not change size of array then they can be 0 or undefined(null). So I think it differs by how you program the function that returns array.
your array is not dynamic so you just can't reduce its size after its been allocated.setting the value zero might solve the problem in your case.

Ways to fill a hole of length L with sticks of lengths s and t

I need help to modify the solution I came up with for a programming challenge. The problem statement says as follows:
Martin the zebra of Madagascar (the movie) wants to fill the hole that's left to cover in the floor of the hut that is building in the edge of the beach. The hole has length L and Martin has many pieces of wood, some with length s and others with length t. As Martin is very distracted he wants to know in how many ways the hole can be filled by putting pieces of wood at will.
Input specification
The only line of input contains three integers L, s and t separated with a space (1 <= L, s, t <= 10^6, s != t).
Output specification
A line with the number of different ways to fill the hole modulo 10^9 + 7 (1000000007).
Sample input
6 2 3
Sample output
2
The solution I submitted, uses this function to count:
#include <iostream>
#include <vector>
using namespace std;
int ** create(int n, int m) {
int ** a = new int*[
for (int i = 0; i < n; i++) {
a[i] = new int[m];
a[i][0] = 1; // I assumed there is one way to fill a hole of length zero
}
return a;
}
int count(vector<int> stick, int n, int m) { // Counts ways to fill the hole
int ** fill = create(n + 1, m + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (j < stick[i - 1])
fill[i][j] = fill[i - 1][j] % 1000000007;
else
fill[i][j] = (fill[i - 1][j] + fill[i][j - stick[i - 1]]) % 1000000007;
return fill[n][m];
}
int main() {
int l, a, b;
cin >> l >> a >> b;
vector<int> stick{a, b};
cout << count(stick, stick.size(), l) << endl;
return 0;
}
The problem is that this only counts the different sets that can fill the hole completely, for example:
Say we have a hole of length L = 6 and sticks of lengths s = 1 and t = 2, my function returns 4. This are the four sets that my function is counting:
{1, 1, 1, 1, 1, 1}
{1, 1, 1, 1, 2}
{1, 1, 2, 2}
{2, 2, 2}
But what it's required are all the permutations of this sets, hence this should return 13, that is:
{1, 1, 1, 1, 1, 1}
{1, 1, 1, 1, 2}
{1, 1, 1, 2, 1}
{1, 1, 2, 1, 1}
{1, 2, 1, 1, 1}
{2, 1, 1, 1, 1}
{1, 1, 2, 2}
{1, 2, 1, 2}
{2, 1, 1, 2}
{1, 2, 2, 1}
{2, 1, 2, 1}
{2, 2, 1, 1}
{2, 2, 2}
How can I modify my function to count all the permutations? Is there any material that can help me understand how to build a dynamic programming solutions for this kind of problems?
let d[i] - number of ways to fill the hole of length i
then d[i] = d[i-s] + d[i-t]
d[0] = 1
d[i < 0] = 0 obviously

How can I average a subset of an array and store the result in another array?

I have a C array fftArray[64] that contains values that I want averaged and placed into another array frequencyBar[8]. Getting the average of the entire array would be easy enough using a for statement.
int average, sum = 0;
for (i = 0; i < 64; i++)
{
sum += fftArray[i];
}
average = sum/64;
But I just can't seem to figure out how to get the average from fftArray[0] through fftArray[8] and store this in frequencyBar[0], the average of fftArray[9] through fftArray[16] and store this in frequencyBar[1], etc. Can anyone help me out with this? Thanks
This looks like a homework assignment, so, rather than give you the outright answer, I'd rather just point you in the right direction...
use a nested loop (one inside the other). One loop cycles 0-7, the other one 0 - 63. Use the smaller one to populate your sliced averages.
or better yet use the % operator to see when you've gone through 8 elements and do an average of your total, then reset the total for the next set. Then you'll have learned how to use the % operator too! :)
[EDIT]
ok, if not homework then something like this... I haven't written C in 5 years, so treat this as pseudo code:
//assuming you have a fftArray[64] with data, as per your question
int i,sum,avCounter,total;
int averages[8];
for(i=0 , avCounter=0, total=0 ; i<64; ){
total += fftArray[i];
if(++i % 8 == 0){ //%gives you the remainder which will be 0 every 8th cycle
averages[avCounter++] = total / 8
total = 0; //reset for next cycle
}
}
I think this will work better than a nested loop... but I'm not sure since % is division which is more processor heavy than addition... however... I doubt anyone would notice :)
int i, j;
for (i = 0; i < 8; i++) {
int sum = 0;
for (j = 0; j < 8; j++) {
sum += fftArray[ 8*i + j ];
}
frequencyBar[i] = sum / 8;
}
Bonus exercise: Optimize this code for speed on your chosen platform.
TF,
DISCLAIMER: This code is just off the top of my head... it hasn't even been compiled, let alone tested.
// returns the average of array[first..last] inclusive.
int average(int[] array, int first, int last) {
int sum = 0;
for (i = first; i <= last; i++)
sum += array[i];
return sum / (last - first + 1); // not sure about the +1
}
Then what you'd do is loop through the indexes of your frequencyBar array [0..7], setting frequencyBar[i] = average(array, first, last);... the tricky bit is calculating the first and last indexes... try i*8 and (i+1)*8 respectively... that may not be exactly right, but it'll be close ;-)
Cheers. Keith.
EDIT: Bored... waiting for my test results to come back. No news is good news, right? ;-)
It turns out that passing the length is a fair bit simpler than passing the last index.
#include <stdio.h>
int sum(int array[], int first, int length) {
int sum = 0;
for (int i = first; i < first+length; i++)
sum += array[i];
return sum;
}
double average(int array[], int first, int length) {
double total = sum(array, first, length);
#ifdef DEBUG
printf("DEBUG: [%2d..%2d] %d", first, first+length-1, array[first]);
for (int i = first+1; i < first+length; i++)
printf(" + %d", array[i]);
printf(" = %d / %d = %f\n", (int)total, length, total/length);
#endif
return total / length;
}
int main(int argc, char* argv[]) {
int array[] = { // average
1, 2, 3, 4, 5, 1, 2, 3, // 2.625
4, 5, 1, 2, 3, 4, 5, 1, // 3.125
2, 3, 4, 5, 1, 2, 3, 4, // 3
5, 1, 2, 3, 4, 5, 1, 2, // 2.875
3, 4, 5, 1, 2, 3, 4, 5, // 3.375
1, 2, 3, 4, 5, 1, 2, 3, // 2.625
4, 5, 1, 2, 3, 4, 5, 1, // 3.125
2, 3, 4, 5, 1, 2, 3, 4 // 3
};
double frequency[8];
for (int i = 0; i < 8; i++)
frequency[i] = average(array, i*8, 8);
for (int i = 0; i < 8; i++)
printf("%f ", frequency[i]);
printf("\n");
}
Watch your sum doesn't wrap around if fftArray has large value in!

Resources