C - CS50 pset1 Mario Pyramid challenge - c

I've googled and read posts on stacks, but still don't know where my codes goes wrong on this problem set... it just keep asking typing "height". After typed height in, it doesn't go to next step, seems to stuck in an infinite loop.
Brain melting.
Please help to point out where my codes goes wrong. Thx a lot.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
printf ("height: ");
height = get_int();
}
while (height >= 0 || height <= 23);
for (int loop = 0; loop <= height; loop++)
{
for (int space = height - 1 - loop; space > 0; space--)
printf(" ");
for (int hash = 2 + loop; hash < height; hash++)
printf("#");
printf("\n");
}
}

Here, in the do while loop you are giving the condition that loop should continue whatever you put the height because every number is either >=0 or <=23.
Instead change the condition to while(height<0||height>23)

Related

Why is this do while loop not working in c?

Sorry for what may be a commonly asked question but my code is a lot simpler to any other questions I've seen and it still isn't working.
My code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Width: ");
}
while (n < 1);
}
This code is an exact copy from Harvard's cs50 course. What I expect is if n is less than 1, it will prompt the user again until a value of 1 or above is entered. However, it is only asking me for width once even when I enter 0 and finishing.
This is the way to handle this kind of issues:
Either you start using a debugger and put a breakpoint at the following line:
int main(void)
{
int n;
do
{
n = get_int("Width: ");
} // here you set your breakpoint
while (n < 1);
}
You add a watch for the variable n and check the value.
Another approach is the following:
int main(void)
{
int n;
do
{
n = get_int("Width: ");
printf("The value of n is [%d]", n); // this line shows the value of n
}
while (n < 1);
}
Bottom line: you need to investigate the value of n, either using a debugger, either showing it on screen.

My C program to find closest pair of numbers from user input is not printing the right output?

I am trying to find the closest pair of numbers entered by the user. My C code isn't working right and I can't figure out what's wrong. I think it might have something to do with storing the values but I don't know where to go from here.
#include <stdio.h>
#include <math.h>
int main()
{
int i, j,arr[50], first,second;
//loop input
for(i=0;i<50;i++) //loop 50 times
{
scanf("%d", &i); //scan
//break if i=-1
if (i==-1)
break;
//if not print
}
//2nd num - 1st num < 3rd num-1st num, closest = 1st and 2nd num
//i[0]=num1, j[0+i]=2nd num, i= 4 , 5, 7, ans=arr,
//if j[0+i]-i[0]= ans < j[0+i]-i[i]=ans
//arr[i]=8,2,17,4,25
for(i=0;i<50;i++)
{
for(j=i+1;j<50;j++)
{
if(arr[j]-arr[i]<arr[j+1]-arr[i])
{
first = arr[i];//3
second = arr[j+1];//5
}
}
}
printf("%d %d\n", first, second);
return 0;
}
Don't post it as answer, prefer editing your code instead. Anyway, the problem is here :
for (j = i + 1; j < len; j++)//j<i <-why is it wrong?
How isn't it wrong? You've initialised j with the value i+1. How's it supposed to be ever less than i? And due to that, it's picking up values from outside the array and providing you with unexpected results.
The correct form is :
for (j = 0; j < i; j++)
The problem is with this chunk of code. You're scanning in the counter variable i instead of array. And then you're manipulating stuff using array arr. Why should that work in any scenario?
for(i=0;i<50;i++) //loop 50 times
{
scanf("%d", &i); //scan
//break if i=-1
if (i==-1)
break;
//if not print
}
And i can never be -1 unless it's a miracle.

Printing a triangle with 3 different symbols

I've been sent a exercise where I have to print to the console (in C) a triangle using three different symbols in the following way:
The program must ask how many rows the triangle will have.
The program will print the triangle in the following way:
From outside to inside, the first triangle will be formed of *, the one inside it will be -, the one inside again will be $ and then the other way around...
This is my code:
#include <stdio.h>
int num = 0; //Var to store the user input.
int blank; //Var to store number of blank spaces.
int main() {
printf("Number of rows? ");
scanf("%d", &num);
printf("\n");
blank = num - 1;
//For loop to print each row of the triangle.
for(int k = 1; k <= num; k++) {
//For loop to print each blank space.
for(int j = 1; j <= blank; j++) {
printf(" ");
}
blank--; //Decrease number of blank spaces.
//For loop to print each symbol of the triangle.
for(int r = 1; r <= 2 * k - 1; r++) {
if(r % 2 == 0) {
printf("-");
}
else if(r % 3 == 0) {
printf("$");
}
else {
printf("*");
}
}
printf("\n");
}
blank = 1;
return 0;
}
And this is what it's printing:
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.

Making rectangle based on input

input
6 6 4
output
######
#....#
#....#
#....#
#....#
######
input
7 7 4
output
#######
#.....#
#.....#
#.....#
#.....#
#.....#
#######
This is the code I have so far.
#include <stdio.h>
int main()
{
int width;
int breadth;
scanf("%d", &width);
scanf("%d", &breadth);
if (width == 1 && breadth == 1)
{printf ("#\n");}
else{
for(int i = 0; i<breadth; i++){
for(int j = 0; j<width;j++)
{
{printf("#");}
}
printf("\n");}
}
return 0;}
Does anyone know how to replace the non boundary area with "."?
ps:there is no need to worry about the third input, it does not affect the shape of rectangles since its just going to be used next task.
I am guessing that I could perhaps change my for loops but I ran out of ideas at this point.
It would be greatly appreciated if someone could help me return identical output from above.
Thank you.
You want to replace non-boundary area with ".". To achieve that you need a condition that satisfies your boundary.
#include <stdio.h>
int main()
{
int width;
int breadth;
scanf("%d", &width);
scanf("%d", &breadth);
if (width == 1 && breadth == 1)
{printf ("#\n");}
else{
for(int i = 0; i<breadth; i++){
for(int j = 0; j<width;j++)
{
if(i==0||j==0)
printf("#");
else if(i==(breadth-1)||j==(width-1))
printf("#");
else
printf(".");
}
printf("\n");}
}
return 0;
}
Hope this works. Let me know if it does not or any problem is there.
Here is the working code.
You were almost there. You need the condition statement to check the boundary..
Please see the line that checks the boundary
#include <stdio.h>
int main()
{
int width;
int breadth;
scanf("%d %d", &width, &breadth);
if (width == 1 && breadth == 1)
printf ("#\n");
else
{
for(int i = 0; i<breadth; i++)
{
for(int j = 0; j<width;j++)
//Check for boundary.
if( j == 0 || j == width - 1 || i == 0 || i == breadth - 1 )
printf("#");
else
printf(".");
printf("\n");
}
}
return 0;
}
You should use counters. For example, one counter should be refering to which line you're printing and another one the column you're printing on.
If the line counter is 0, print all the line as '#' cause it's going to be the upper boundary line
(this creates -> #######)
Else If the line counter is same as the height of the shape, then it's the finishing border line
(#######)
Else if it's >0, print the first character as '#', then print '.' until another counter reaches the width of the shape -1 (for example in 7 7 that would be 5 (since 0 also counts)) and then another '#'
(#.....#)
I think this is enough help for you to get it done by yourself, should you require further assistance tho, just ask.
I have check it by gcc ,it works.Please see the notes in codeļ¼š
int main()
{
int width;
int breadth;
scanf("%d",&width);
scanf("%d",&breadth);
for(int i = 0; i < width; i++){
for(int j = 0; j < breadth; j++ ){
//first line and last line
if((i == 0)||i == (width - 1)){
printf("#");
//first column and last column
}else if((j == 0)||(j == (breadth -1))){
printf("#");
//other case
}else{
printf(".");
}
}
printf("\n");
}
return 0;
}
Using conditions as suggested by other answers is the ideal way but if that is unclear, you can break down the printing into smaller loops as
for (i=0; i<width; i++) // prints the first line
printf("#"); //
printf("\n"); //
for (j=0; j<breadth-2; j++){ // prints middle breadth-2 lines
printf("#"); // prints the starting hash in middle lines
for (i=0; i<width-2; i++) // prints the '.'s
printf(".");
printf("#\n"); // prints the last hash in middle lines
}
for (i=0; i<width; i++) // prints the last line
printf("#"); //
printf("\n"); //
Although not very efficient, the logic is very easy to understand.
Let me know if further explanation is required.
Edit: Actually after writing this answer I later realized that this code will infact be faster than the ones with branches. Since we have split the iteration domains ourselves, there are no branches. This reduces any chance of branch prediction failing, which would certainly happen in the solution with if conditions since the condition is true for small cases. Ofcourse it wouldn't matter for small iterations like 6x6, but just putting the thought out there.

How exactly does space work with scanf?

I am a math student, and I'm learning the very basics in programming in C. I need a program to read an input consisting in an array, the components of which must have certain requisites; I would like the program to ask the user for the components of the array. The user should then have to enter such components separating them with spaces. The details aren't important to get the main question across; I'll choose a simpler example then the one I am dealing with: let's say I want an array with 6 components not to contain the number 4. So I tried:
#include <stdio.h>
int main(void) {
int a[6];
printf("enter components: ");
int i;
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
if (a[i] == 4) printf(" \n\n4 is not allowed, try again\n\n");
}
for (i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
}
If I compile this and run it, and for example enter:
1 2 3 4 5 6
I will get my error message, but only after having pressed enter, that is after having entered all six components (not straight after having pressed space for the fourth time). So here are my questions (I am looking for solutions which don't make use of strings or pointers, unless it is impossible to do without them):
Is there a way to get the program to read a component (and to act accordingly) straight after its subsequent space has been entered? I'm guessing there isn't because scanf only works after the user presses enter, and not space, correct?
If there isn't, is there a way to get the program to read the components all at once after having pressed enter at the end, but letting the user pick up from the last right component? For example, with the above input, I would like the program to display something like this:
4 is not allowed
1 2 3 _
so that the user can correct his/her input (possibly changing the first three digits as well).
Sorry if this question is too dumb! Thank you for your help!!
EDIT: Well, thanks for the great answers, you have all been very helpful! It's a pity I can't accept more than one.
In for loop, after each iteration, the counter add by one automatically. If you get an invalid input, you should prevent the counter increasing. To do this, just add i--; to your code when you give an invalid input.
#include <stdio.h>
int main(void) {
int a[6];
printf("enter components: ");
int i;
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
if (a[i] == 4){
printf(" \n\n4 is not allowed, try again\n\n");
i--;
}
}
for (i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
}
Please see the bellow code:
#include <stdio.h>
int main(void) {
int a[6];
int i;
bool hasError = false;
int errorIndex = 0;
do{
hasError = false;
printf("enter components: ");
for (i = 0; i < errorIndex; i++)
printf("%d ", a[i]);
for (i = errorIndex; i < 6; i++) {
scanf("%d", &a[i]);
if (a[i] == 4 && hasError == false){
printf(" \n\n4 is not allowed, try again\n\n");
hasError = true;
errorIndex = i;
}
}
}while(hasError == true);
for (i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
}
This is related to your terminal being in "cooked" mode. Characters aren't even sent to the program until the user presses enter.
You could do something like this:
int i,a[6];
for (int i=0;i<6;i++) {
scan: scanf("%d",&a[i]);
}
for (int i=0;i<6;i++) if (a[i]==4) {
printf("4 is not allowed. re-enter the last %d numbers\n",6-i);
goto scan;
}
note that in most case, it's better to avoid using goto, but in this case I think that it's natural.
If you really want, you can print the first i numbers (before the goto), but it's complicated (and platform-depended) to let the user change those numbers.
Improving on Mir Milad Hosseiny answer (I wrongly identified it as being an out of control infinite loop... it's actually exactly the infinite loop I describe in my comment)...
I would write a small function that has either a "white list" (things you want) or a "black list" things you don't want, and check each value to either belong or not (depending on the approach) to the list. That way you can keep a separate place where your store the values that you are willing to accept or the values you are not, so your primary function doesn't get really messy with exceptions or inclusions in the "if"
so your code would be
if(isAllowed(a[i]){
myList[j] = a[i]; //j is your alternate counter
}

Resources