Cs50 Mario: How can I improve this code? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It took me all day to figure this out but even though I completed it I don't fully understand how it works and I feel there is a better or cleaner way of writing it.
Can some one explain how I can improve my code?
#include<cs50.h>
#include<stdio.h>
int main(void)
{
int rows, height, spaces, hashes;
do
{
printf("Height: ");
height = get_int();
}
while(height < 0 || height > 23);
for(rows = 0 ; rows < height; rows++)
{
for(spaces = height - 1; spaces > rows; spaces--)
{
printf(" ");
}
for(hashes = 0; hashes < spaces + 2; hashes++)
{
printf("#");
}
printf("\n");
}
return 0;
}

It is best if you first try to understand what the code is doing. Work it through on paper, starting with the input 1.
If you are keen you might like to explore the printf width and precision formatting, where each * in the format specifier is replaced by the corresponding function argument. But beware - printf is a complicated and detailed function. This also introduces the array.
char hatch[] = "##############################";
for(rows = 0; rows < height; rows++) {
printf("%*.*s\n", height + 1, rows + 2, hatch);
}
For the input of 1 your program prints
##
For the input of 5 it prints
##
###
####
#####
######
and so does this. The loop contains a single instruction.

Related

In C - How would I use scanf to store a random amount of ints to a 2d array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was wondering how you would do the following:
I provide the width and height of a grid as command line arguments, and I now need to read the grid from standard input and store it into a 2d array. if the command line arguments are just standard integers and the grid is in the following format:
1 2 3 4 5 6 ... x
1 2 3 4 5 6 ... x
1 2 3 4 5 6 ... x
. . . . . . ... x
y y y y y y y y
how would I read this? I'm getting really confused because I don't have a static number of rows and columns since the values of each can change based off user input.
If your question is about creating your grid using command line arguments, indeed, as JohnG said, arguments are static, so your width and height are static and you can simply create an array.
Say your program is run by using the format ./program heigth width, you just have to use:
int tab[atoi(argv[1])][atoi(argv[2])];
However, if you want to dynamically create a two-dimensional array (meaning with a width and heigth you don't know beforehand), here is an example, using malloc and pointers, which are two very importants parts of the C language.
Note that this isn't exactly a two-dimensional array, but a 1D array of pointers and several 1D arrays of int.
#include <stdio.h>
#include <stdlib.h>
int main () {
printf("Height: ");
int height = 0;
scanf("%d", &height);
printf("Width: ");
int width = 0;
scanf("%d", &width);
int** tab = malloc(height*sizeof(int*)); // Take the space for height * pointers of integer that points towards the first values of each line
for (int i = 0; i < height; i++) {
tab[i] = malloc(width*sizeof(int)); // take the space for width * integers, where we will stock our values
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
tab[i][j] = j + 1; // assign the value 1, 2, 3... to each line
}
}
// Print array:
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("%d ",tab[i][j]);
}
printf("\n");
}
// Free memory allocated for each malloc
for (int i = 0; i < height; i++) {
free(tab[i]);
}
free(tab);
return 0;
}

Please Explain me this C code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Can some one please help me on understanding this code especially the 'space' part. How is value of space in spite being 4 printing no space in the first row of the output of this code:
#include <stdio.h>
/* Inverted mirrored right triangle */
int main(int argc, char *argv[])
{
int i, j, space;
for (i = 0; i <= 5 ;i++) {
for (space = 5 - i; space <= 4; space++) {
printf(" ");
}
for (j = 5 - i; j >= 0; j--) {
printf("*");
}
printf("\n");
}
return 0;
}
concentrate on this piece of code
for (i = 0; i <= 5 ;i++) {
--> for (space = 5 - i; space <= 4; space++) {
printf(" ");
}
in first iteration of inner for loop space value is space: 5-0 = 5
as the i:0 and for we have condition space <= 4 and this not satisfy control does not
go in to the inner of for braces and not print any space (for first row)
you must know how the for(;;) works.
for (i=0 ; i<10 ; i++ ) {
printf("%d",i);
}
// end loop line
for (loop index initialize ; condition ; do after innerloop commands ) {
command1;
command2;
}
first of all i:0 and because i is less than 10 inner of loop executes
and print 0 on screen.
after printing i are incremented by one (i++ equals to i = i+1).
then i:1 , i still less than 10 and print i:1, wee see 1 on screen and so
on. until we have i:9 print 9 on screen then increase by one i:10
then last iteration come, as now i:10 and is not less than 10 the inner of
loop not execute, program control comes to after loop (end loop line).

Why are the wrong array values being printed? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

How to print char value in int array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array board[3][3] of type int. I want to show 8 numbers and on the last place "_" must be shown. I don't know how to do that. Please help
If the 8 numbers you want print is the first 8, you can just use a loop to with printf to print it until the last number:
int j, k;
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
if (j == 2 && k == 2)
printf("_");
else
printf("%d ", board[j][k]);
}
printf("\n");
}

CS50 Mario I'm stuck doing that pyramid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Well, I've like a week reading all kind of material regarding to C programming to make this pyramid works and finally I decided to ask for help, I've improve a lot, my code now takes the input (0 to 23) correctly and print a new line, but now that I'm trying to add the # into the code, the code make an infinite loop :(.
#include <stdio.h>
#include <cs50.h>
/* PSET1 Mario */
int main (void)
{
int height;
do
{
printf("Give me an int between 0 or 23 \n");
height = GetInt();
}while((height < 0)||(height > 23));
//The Pyramid output
int row, space;
space = height - 1;
for (row = 0 ; row <= height; row++)
{
for (space = 0; space < row; space--)
{
printf("#");
}
return 0;
}
The main point it is do a pyramid with ''#'' and I edited the post this was my previous code with the #, it was doing a infinite loop.
Is this what you want?
#include <stdio.h>
#include <cs50.h>
/* PSET1 Mario */
int main (void)
{
int height;
do
{
printf("Give me an int between 0 or 23 \n");
height = GetInt();
}while((height < 0)||(height > 23));
//The Pyramid output
int row, space;
space = height - 1;
for (row = 0 ; row <= height; row++)
{
int simbol=0;
for (space = height-row; space > 0; space--)
{
printf(" ");
}
for (simbol = 0; simbol < row; simbol++)
{
printf("##");
}
printf("\n");
}
return 0;
}
This give you this pyramid:
##
####
######
########
##########
############
##############
################
##################
####################
######################
########################

Resources