My reverse triangle has an extra dot in the middle - c

As the title states, there seems to be an extra dot on my reverse triangle, but it is only apparent on the last line of the triangle. I'm very new to programming and would appreciate any help I could get. For practice purposes, I would appreciate it if the code remained as a do-while loop. Thank you!
Here is my code:
#include <stdio.h>
int main(){
int rows, rows1, a, b, c, d;
printf("Enter number of rows:");
scanf("%d", &rows);
rows1 = rows;
a = 1;
do{
b = 1;
do{
printf(" ");
b++;
}while(b <= a);
c = 1;
do{
printf("*");
c++;
}while(c <= rows1);
c = rows - a;
do{
printf("*");
c--;
}while(c >= 1);
rows1--;
printf("\n");
a++;
}while(a <= rows);
return 0;
}

notice here
do {
printf("*");
c++;
} while (c <= rows1);
c = rows - a;
do {
printf("*");
c--;
} while (c >= 1);
these loops both print at least one star, you print * then test to see if thats enough. So you will always get at least 2 stars on a line.
You need to change to a loop that test first, ie while
note - this is easy to see if you set number of rows to just 1

Related

Trouble with a C program

I had a homework. The condition was: "Make a program in witch will be displayed how many 'A' and 'B' where typed". I tried and this is what i made:
#include <stdio.h>
int main()
{
int n, i, a=0, b=0;
char cr;
scanf ("%i", &n);
for (i=0; i<=n; i++) {scanf ("%c", &cr); if (cr='A') a++; else if (cr=='B') b++;}
printf ("A-%i\n", a);
printf ("B-%i", b);
}
The problem is when i type for example 10, it only lets me type 5 characters, not 10. Why?
The program can be made with for, while or do while.
Problems and solutions
you need to use scanf(" %c", %cr) to prevent the program from reading trailing whitespaces
You should be using if (cr == 'A') and not if (cr = 'A') because you are comparing and not assigning
the loop should be for (i = 0; i < n... not for (i = 0; i <= n.. as the loop would ask for 1+ input from the specified range
#include <stdio.h>
int main() {
int n, a = 0, b = 0;
char cr;
printf("Enter number of tries :\n");
scanf ("%i", &n);
for (int i = 0; i < n; i++) {
printf("Enter char\n");
scanf(" %c", &cr);
if (cr == 'A') a++;
else if (cr == 'B') b++;
}
printf ("A-%i\n", a);
printf ("B-%i", b);
}

Understanding of this if statement

I created a finished code for a mario problem set.
Want to know if I understand the last if statement correctly.
If the user enters 4, the code will print out 4 columns with hashes, because it uses the remaining rows to print a blank space on each column.
Just wanted to know, if my point of view is correct.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int r, c, ans;
printf("Value between 0 and 9\n");
scanf("%d", &ans);
while (ans < 1 || ans > 8) {
printf("INVALID REPSONSE PLEASE TRY AGAIN\n");
scanf("%d", &ans);
}
if (ans > 0 || ans <= 8) {
for (r = 0; r <= ans; r++) {
for (c = 0; c < ans; c++) {
if (c >= ans - r) {
printf("#");
} else {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}

NULL confusion in C

I am creating a simple "ascending" program. When I find smallest int in array of int I want to replace it with some other value so it won't come as smallest number again in the array. For that, I assigned that int NULL. But now the results are not as expected.
Please tell me if I am doing anything wrong. If so then what I should replace with the value of that int ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],b,c=0,d=0;
printf("Enter number of values you want to enter \n");
scanf("%d",&b);
printf("Enter values \n");
for(int i=0;i<b;i++)
scanf("%d",&a[i]);
while(c<b)
{
for(int k=0;k<b;k++)
{
for(int j=0;j<b;j++)
{
if(a[k] > a[j])
{
d=1;
}
}
if(d!=1 && a[k]!=NULL)
{
c++;
printf("%d ",a[k]);
a[k]='\0' ; //assigning it as NULL
}
if(c >= b)
break;
d=0;
}
}
getch();
}
In C and related languages ints are not "nullable" - you could use a special value instead, e.g. a value that is well outside the expected range of your input data, such as INT_MAX:
#include <limits.h> // required header for INT_MAX et al
...
if(d!=1 && a[k]!=INT_MAX)
{
c++;
printf("%d ",a[k]);
a[k]=INT_MAX
}
However it would probably be a good idea to go back to the drawing board and see if you can come up with a better algorithm that doesn't require special values.
Read the differences between NULL and 0 and '\0' here. There is a type mismatch when you're trying a[k]!=NULL.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a[10], b, c = 0, d = 0;
int k, j, i;
printf("Enter number of values you want to enter \n");
scanf("%d",&b);
printf("Enter values \n");
for(i = 0;i < b;i++)
scanf("%d",&a[i]);
while(c < b)
{
for(k = 0;k < b;k++)
{
for(j = 0;j < b;j++)
{
if((a[k] > a[j]) && a[j] != 0)
{
d=1;
}
}
if(d != 1 && a[k] != 0)
{
c++;
printf("%d ",a[k]);
a[k] = 0; //assigning it as NULL
}
if(c >= b)
break;
d=0;
}
}
return 0;
getch();
}
This code fixes the problem.
What you're missing is a[j] != 0 in if((a[k] > a[j]) && a[j] != 0). Also I don't suggest this, as it won't work if there are 0's in the array entered.

How to alter my program to use while loops instead of for loops. Asterisk triangle in c

For my programming assignment I have to create 3 programs that print out an asterisk based triangle in c based on the user's input. The difference between the 3 programs would be one will use for loops, the other would use while loops and the last one would use goto. I have the for loop program as well as the goto program, but as for the while loop program I'm not sure how incorporate it into my program. This is my program with a for loop and the second program is my attempt at the while loop version.
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Progam #2:
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
a++;
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Here is how you convert a for loop like the following
for (stat1; stat2; stat3) {
stat4;
}
to a while loop
stat1;
while (stat2) {
stat4;
stat3;
}
So here is the while loop you want:
a = 1;
while(a <= lines) {
b = 1;
while (b <= a) {
printf("*");
b++;
}
printf("\n");
a++;
}
Set b=1 before 2nd while loop
while(a <= lines) {
a++;
b=1; //you want to start b from 1 for each inner loop
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
The program2 can be changed as below. The below code result is equivalent to program1.`
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
//a++;
while (b <= a) {
b++;
printf("*");
}
b =1;
a++1;
printf("\n");
}
} while(1);
system("pause");
}`

2 dimensional character array

#include <stdio.h>
#include <stdlib.h>
#define rsize 3
#define csize 3
int
main()
{
char tictac[rsize][csize];
int a, b;
printf("WELCOME TO TIC TAC TOE \n");
for(a = 0; a < rsize; a++)
{
for(b = 0; b < csize; b++)
{
printf("Enter X or O: ");
scanf(" %c", &tictac[a][b]);
}
}
for(a = 0; a < rsize; a++)
{
if (tictac[a][0] == tictac[a][1] && tictac[a][1] == tictac[a][2]);
{
printf("Row %d has all %c's \n", a, tictac[a][0]);
}
if (tictac[0][a] == tictac[1][a] && tictac[1][a] == tictac[2][a]);
{
printf("Column %d has all %c's \n", a, tictac[0][a]);
}
}
system("pause");
return(0);
}
It's supposed to be a 3x3 tictac toe game but it doesn't seem to be working. The problem is the if statement I'm not sure why it doesn't work. Come someone help me out and point out my problem?
You have extra semicolons in the middle of the if statements that cause them to be empty. So your code is effectively
if (...) {
/* do nothing */
}
{
printf(...
}
and the printfs are always executed. Get rid of the ';' between the ')' and the '{'
After IF( condition), you have placed a semicolon(;).
Remove that.

Resources