I have this array and I have to make pattern like this - arrays

array = [2, 4, 3, 9, 6, 5];
and we have to print like this
*
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
I have tried the normal nested for loop 1st loop for row, 2nd for spaces, and third for printing star.

In C# it could look like:
var height = array.Max();
for (int i = 0; i < height; i++)
{
Console.WriteLine(string.Join(" ",
array.Select(x => x >= height - i ? "*" : " ")));
}

Related

how to do this in c: error: invalid operands to binary expression ('char [3]' and 'int')

wanted:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
my code
//THIS CODE IS NOT WORKABLE
#include<stdio.h>
int main() {
char arr[] = "* * * * *";
printf("%s\n", arr);
for (int i=1; i<5; i++) {
char sp[] = " "*i;
printf("%s\n", sp[i]+arr);
}
return 0;
}
i do this in python:
aa = '* * * * *'
print(aa)
for i in range(1,5):
print(' '*i + aa)
--> how to fix this in c? looking forward some python-like style code in c(as simple as possible)
There is no an equivalent binary operator * in C as in Python that allows to propagate a string.
But in any case your approach even in Python is not flexible because it deals with the fixed string "* * * * *".
To output the pattern in C you should use loops as for example shown in the demonstration program below.
#include <stdio.h>
int main( void )
{
while ( 1 )
{
const char c = '*';
printf( "Enter a non-negative number (0 - exit ): " );
int n = 0;
if ( scanf( "%d", &n ) != 1 || n <= 0 ) break;
putchar( '\n' );
for ( int i = 0; i < n; i++)
{
printf( "%*c", 2 * i + 1, c );
for ( int j = 1; j < n; j++ ) printf( " %c", c );
putchar( '\n' );
}
putchar( '\n' );
}
}
The program output might look like
Enter a non-negative number (0 - exit ): 1
*
Enter a non-negative number (0 - exit ): 2
* *
* *
Enter a non-negative number (0 - exit ): 3
* * *
* * *
* * *
Enter a non-negative number (0 - exit ): 4
* * * *
* * * *
* * * *
* * * *
Enter a non-negative number (0 - exit ): 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Enter a non-negative number (0 - exit ): 0

Trying to write a code that outputs a checkerboard in C

I'm trying to write a code that outputs a checkerboard type effect with astericks by using for loops. I cant seem to get it to work. Here's my code. Any help is greatly appreciated.
#include <stdio.h>
int main (void)
{
int a;
int b;
for(a=1; a<=10; a+=2)
{
printf("* * * * * * * * * *");
}
for (b=2; b<=10; b+=2)
{
printf(" * * * * * * * * * ");
}
return 0;
}
You need to alternate each row
#include <stdio.h>
int main (void) {
int a;
for(a=1; a<=10; a+=2) {
printf("* * * * * * * * * *\n");
printf(" * * * * * * * * * \n");
}
return 0;
}
Try to use a single loop and a % 2 to alternate between your 2 types of lines:
#include <stdio.h>
int main (void)
{
for (int a = 1; a <= 10; a += 1)
{
if (a % 2 == 0)
printf(" * * * * * * * * * \n");
else
printf("* * * * * * * * * *\n");
}
return 0;
}
Here is my solution to this, trying to keep it as simple as possible
#include<stdio.h>
int main() {
// We want 10 checkerboard lines, so we iterate from 0 through 9
for(int a = 0; a < 10; ++a) {
// We want to change the pattern any other line, an easy way to do this is to
// branch on whether the iterator index, a, is even (a%2 = 0), or odd (a%2 != 0).
if(a%2){
// If a is odd we print the shifted pattern, with a newline
printf(" * * * * * * * * * \n");
}else {
// if a is even, we print the normal pattern
printf("* * * * * * * * * *\n");
}
}
}
well you are printing all the odd lines and then all the even lines of your check board. moreover you miss the carriage return. you could try with one loop only.
#include <stdio.h>
int main (void)
{
for(int a=1; a<=10; a+=2)
{
printf("* * * * * * * * * *\n")
printf(" * * * * * * * * * \n")
}
return 0;
}
While there is nothing wrong with printing a fixed-size board, why limit yourself to output a string-literal? Computers are about solving problems in a flexible way. What if instead of wanting 9-rows of 10/9-asterisk, you wanted to double the size of the board? Or, add one more column and two rows? You would be stuck changing your string literals an loop conditions.
Let the computer handle figuring out what to print based on the simple input of the number of rows? Now the number has to be an odd number, so you get to decide to add one or subtract one if an even number of row are entered, but other than that, write an algorithm that handles the rest. It takes little extra effort, e.g.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 9
#define COLS ROWS
int main (int argc, char **argv) {
int rows = argc > 1 ? atoi (argv[1]) : ROWS,
cols = rows & 1 ? rows : (rows += 1); /* must be odd */
for (int i = 0; i < rows; i++) { /* output rows */
if ((i & 1) == 0)
putchar ('*');
for (int j = 0; j < cols; j++) /* output cols */
fputs (" *", stdout);
putchar ('\n');
}
return 0;
}
(note: you should use strtol instead of atoi and do error checking on the input, and you should check that the argument given is non-negative and limit it to say 101 or something likely to fit within your terminal width/height, but those are left to you.)
Example Use/Output
$ ./bin/checkers
* * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Double size board:
$ ./bin/checkers 19
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
The point being when you design your code, make it as robust and flexible as possible. Don't limit your code when you can accomplish the same thing in a way that provides a flexible solution.

hangman program (C language) array issue

Here is the code I wrote to prepare the hangman game in C language using arrays. It runs but gives me incorrect output. Would love for some fresh eyes to tell me where I may have messed up.
#include<stdio.h>
#include<string.h>
void Instructions();
void PlayGame();
void PrintToLog(char *word);
int main(char word)
{
Instructions();
PlayGame();
PrintToLog(word);
return 0;
getchar();
}
void Instructions()
{
printf("This is a game of hangman. Attempt to guess secret word\n");
printf("by entering a letter from a to z. The game is over once you\n");
printf("have entered 8 incorrect guesses.\n\n");
}
void PlayGame()
{
char word[12];
char guessed[12];
int i, incorrect_count, found;
char ch[2];
strcpy(word, "hello");
PrintToLog(word);
for (i = 0; i < strlen(guessed); i++)
{
*(guessed + i) = '*';
}
incorrect_count = 0;
while (incorrect_count < 8 && strcmp(guessed, word) != 0)
{
for (i = 0; i < strlen(guessed); i++)
printf("%c ", guessed[i]);
printf("\n");
printf("Enter your guess:");
gets(ch);
found = 0;
for (i = 0; i < strlen(word); i++){
if (ch[0] == *(word + i))
{
*(guessed + i) = ch[0];
found = 1;
}
}
if (found == 0)
incorrect_count++;
}
if (incorrect_count < 8)
{
printf("\nThe word is %s. You win!", word);
getchar();
}
else
{
printf("\nThe correct word is %s. You lose!", word);
getchar();
}
return 0;
}
void PrintToLog(char *word)
{
FILE *pOutput;
pOutput = fopen("MyLogFile.txt", "w+");
if (!pOutput) return;
fprintf(pOutput, "Start of game\n");
fprintf(pOutput, "This is the word player is trying to guess: %s\n", word);
fclose(pOutput);
}
The incorrect output I get is:
This is a game of hangman. Attempt to guess secret word by entering a letter from a to z. The game is over once you have entered 8 incorrect guesses.
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: k
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: l
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: u
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: h
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: e
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: o
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: k
"* * * * * * * * * * * * * * * * * * * * * * * * *"
Enter your guess: l
The correct word is *****. You lose!
THERE SHOULD only be (5) * * * * * above the guess line for each letter of the word "hello" that is being guessed, but instead 25 * show up. And then on the very last line it should say "The correct word is hello. You lose!" but it doesn't as something is wrong in my program and I can't figure it out. I created a pointer version of the program and it works just fine. But I cannot find where I went wrong on this version.

Asterisk Histogram for random dice rolls of 3 dice

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main()
{
int i;
int d1, d2, d3;
int a[16];
srand(time(NULL));
for(i = 0; i <= 15; i++)
a[i] = 0;
for(i = 0; i < 1000; i = i + 1)
{
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
d3 = rand() % 6 + 1;
++a[d1 + d2 + d3 - 3];
}
char asterisks[0x400];
memset(asterisks, '*', sizeof(asterisks));
for(i = 0; i <= 15; i = i + 1)
{
printf("%3d - ", i+3);
for(j=0;j<a[i];j++)
{
printf("%c ",'*');
}
printf("\n");
}
return 0;
}
Updated code.
The goal is to have an asterisk histogram judging the dice rolls of 3 dice rolled 1000 times. It is to count how many combinations of sums between 3 and 18.
Histogram output should look like this:
Frequency of Results for 3d6
3 - *
4 - **
5 - ****
6 - *******
7 - *********
8 - ***********
9 - ************
10 - ************
11 - *************
12 - **********
13 - *************
14 - ********
15 - ******
16 - ***
17 - ***
18 - **
This is my output as of now:
3 - * *
4 - * * * * * * * * * * * * * * * * * * * *
5 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * *
7 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * *
8 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * *
9 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
11 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * *
12 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * *
13 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * *
14 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * *
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * *
16 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
17 - * * * * * * * * *
18 - * * * * * * * *
3-18 You need array with the size of 16
int a[16];
a[0] indicates Number of 3 counts and
a[1] indicates Number of 4 counts and
....
a[15] indicates Number of 18 counts.
You need to change this for loop
for(i = 0; i <= 15; i++)
a[i] = 0;
and this statement
a[d1 + d2+d3] = a[d1 + d2 + d3] + 1;
Modify like this
++a[d1 + d2 + d3 - 3];
And final for loop also, you need two loops.
for(i = 0; i <= 15; i = i + 1)
{
printf("%3d - ", i+3);
for(j=0;j<a[i];j++)
{
printf("%c ",'*');
}
printf("\n");
}
You can use memset to fill a buffer with asterisks and "%*s" printf format to print as many as you need:
char asterisks[0x400];
memset(asterisks, '*', sizeof(asterisks));
for(i = 3; i <= 18; i = i + 1)
{
printf("%d - %*s\n", i, a[i], asterisks);
}
And yeah as mentioned in the comments you should define a long enough:
int a[19];
The way to fill a char array with asterisks can be done in various ways, a few of which have been explained here, a question that was posted but a few hours ago...
On a different matter:
You're calling the rand function, without providing a seed... rand will then, by default, behave as though it was passed 1 as a seed, and your compiled program will produce not-so-random results.
Consider adding this:
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
int d1, d2;
int a[13];
srand(time(NULL));//seed current time
}
That, and, as Jongware pointed out:
d1 = rand() % 6 + 1;
d2 = rand() %6 + 1;
just doesn't make sense:
d1 = rand() % 18 + 1;
Does just the same thing, requiring only 1 function call. But still, there is another issue with what you're doing with this int value:
You're also using an array of ints (a[13]), the declaration of which implies that the highest offset you can use is 12, the lowest (as ever) is 0. Yet, your loops start at offset 3, and go all the way up to 18...
I know this site is called stackoverflow, but your code seems to be actively working towards Buffer overflow. Fix that, please...

Performance down

Can anyone help me with this?
void _vect_mat(float *vect,float **mat){
float temp[4];
temp[0] = vect[0];
temp[1] = vect[1];
temp[2] = vect[2];
temp[3] = vect[3];
vect[0] = (temp[0] * mat[0][0]) + (temp[1] * mat[1][0]) + (temp[2] * mat[2][0]) + (temp[3] * mat[3][0]);
vect[1] = (temp[0] * mat[0][1]) + (temp[1] * mat[1][1]) + (temp[2] * mat[2][1]) + (temp[3] * mat[3][1]);
vect[2] = (temp[0] * mat[0][2]) + (temp[1] * mat[1][2]) + (temp[2] * mat[2][2]) + (temp[3] * mat[3][2]);
vect[3] = (temp[0] * mat[0][3]) + (temp[1] * mat[1][3]) + (temp[2] * mat[2][3]) + (temp[3] * mat[3][3]);
}
int main(){
int i,j,k;
float *vect,**mat;
vect = (float *)malloc(4 * sizeof(float));
mat = (float **)malloc(4 * sizeof(float *);
for(i=0;i<4;i++)mat[i] = (float *)malloc(4 * sizeof(float));
vect[0] = 1.0;
vect[n] = ......etc.
mat[0][0] = 1.0;
mat[n][m] ......etc.
while (1){
for(i = 0;i < 5;i++){
for(j = 0;j< 6;j++){
for(k = 0;k < 3600;k++)_vect_mat(vect,mat);
}
}
}
}
when I call the function _vect_mat inside the loop, all performance falls. It is Normal?
What am I doing wrong?
One problem is you say float **mat. That means fetching elements like mat[1][2] is a two-step process. First it has to fetch mat[1] as a pointer, and then it has to fetch element [2] from that.
If instead you declare float mat[16], and index directly into that, for example mat[1 + 2*4] the fetching of those elements will be maybe twice as fast.

Resources