Actually this is a quiz from first week of cs50 course. I thought I wrote the code correctly!
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
n = get_int("Enter a number: ");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("#");
}
printf("/n");
}};
};
output should be:
#
##
###
####
and so on...
I don't know what is wrong with my code. can anyone help please.
Your inner loop needs to loop i times for each i.
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("#");
}
printf("\n");
}
A couple of notes:
; is not needed to end a {...} clause
It is not /n, it is \n for a newline character
int main(void) requires a return statement
the statement:
for(int j = 0; j < n; j++){
Should be:
for(int j = 0; j <= i; j++){
Putting it all together:
(Edited to address question in comments.)
int main(void)
{
int n = 0;//initialize n to zero (< 1)
while(n < 1 || n > 8)//stay in while loop until criteria is met
{
n = get_int("Enter a number between 1 and 8: ");
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){//n does not change, i does
printf("#");
}
printf("\n");
}
return 0;
}
Related
I'm making a program that multiplies 3 matrices and print the outcome. The program can input several cases of multiplication and each case can determined their own number of NxN matrix (n). However, if I input 2 cases, first with n=2 and second with n=3, the output of the first case will have a 3x3 matrix with row 3 and column 3 only have 0s. How do I fix this problem?
#include <stdio.h>
int main(){
int t, n, i, j, k, l, a[50][50][10], b[50][50][10], c[50][50][10], d[50][50][10], e[50][50][10];
scanf ("%d", &t);
for (l=1; l<=t; l++){
scanf("%d", &n);
// matrix a
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j][l]);
}
}
// matrix b
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j][l]);
}
}
// matrix c
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & c[i][j][l]);
}
}
//Multiplication
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
d[i][j][l] = 0;
for (k = 0; k < n; k++) {
d[i][j][l] += a[i][k][l] * b[k][j][l];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
e[i][j][l] = 0;
for (k = 0; k < n; k++) {
e[i][j][l] += d[i][k][l] * c[k][j][l];
}
}
}
}
//Printing the product
for (l=1; l<=t; l++){
printf ("Case #%d:\n", l);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", e[i][j][l]);
}
printf("\n");
}
}
return 0;
}
This is the expected output.
My code never stops asking for input so I think I must've made an infinite loop, but I can't find where the error is. I've also noticed that when inserting the input line by line, it prints the result of one loop after inserting the first line of the second loop, which seems incorrect to me. Please help me debug. (For further context, the code is supposed to receive a number we'll call n, and then scan 3n more lines, which are basically n bundles of 3 similar lines. The 2nd and 3rd lines are two words with the same num of characters, and the 1st line is that num. The output is whether or not these words are anagrams.)
#include <stdio.h>
int main() {
int n, l;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
scanf("%d\n", &l);
char A[l], B[l];
for (int j = 0; j < l; j++) {
scanf("%c", &A[j]);
scanf("\n");
}
for (int j = 0; j < l; j++) {
scanf("%c", &B[j]);
scanf("\n");
}
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = 0; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
Example:
input:
2
6
listen
silent
4
Evil
live
output:
YES
NO
It's because you are asking for input over and over and over again,
Not sure what you are trying to achieve, but start by removing the extra scanfs
try this,
#include <stdio.h>
int main() {
int n, l;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
char A[l], B[l];
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = 0; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
Your scanf calls wait for an extra \n which requires more input to enter.
To fix this, remove the \n from your scanf calls. Also remove the extra calls when you enter A and B:
I have added some debug code to demonstrate where you are in your program execution while you enter your input.
#include <stdio.h>
int main() {
int n, l;
int res;
res=scanf("%d", &n);
printf ("res=%d, n=%d\n", res, n);
for (int i = 1; i <= n; i++) {
res = scanf("%d", &l);
printf ("res=%d, l=%d\n", res, l);
// char A[l], B[l];
char A[l+1], B[l+1];
for (int j = 0; j < l; j++) {
scanf(" %c", &A[j]);
}
printf ("A done\n");
for (int j = 0; j < l; j++) {
scanf(" %c", &B[j]);
}
printf ("B done\n");
A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
...
Now your input should work properly.
But your code also contains another error.
You will treat "12344" and "11234" as correct match wchich is wrong.
To fix this you need to remove each matching character from B:
for (int k = 0; k < l; k++) {
int result = 0;
// We compare A[k] with the remaining characters in B only
for (int j = k; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
B[j] = B[k]; // Replace matching character with non-matching character we checked earlier.
break;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
This code stops as soon as the first match is found and removed that character from B.
The entries in B are rearranged to keep the unused entries at the end.
The full code looks like this:
#include <stdio.h>
int main() {
int n, l;
int res;
res=scanf("%d", &n);
printf ("res=%d, n=%d\n", res, n);
for (int i = 1; i <= n; i++) {
res = scanf("%d", &l);
printf ("res=%d, l=%d\n", res, l);
// char A[l], B[l];
char A[l+1], B[l+1];
for (int j = 0; j < l; j++) {
scanf(" %c", &A[j]);
}
printf ("A done\n");
for (int j = 0; j < l; j++) {
scanf(" %c", &B[j]);
}
printf ("B done\n");
A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = k; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
B[j] = B[k];
break;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
output:
~/stackoverflow$ ./test
4
res=1, n=4
2
res=1, l=2
12
A done
21
B done
A="12" - B="21"
YES
6
res=1, l=6
liver1
A done
evil1r
B done
A="liver1" - B="evil1r"
YES
3
res=1, l=3
111
A done
111
B done
A="111" - B="111"
YES
4
res=1, l=4
abcd
A done
dcbb
B done
A="abcd" - B="dcbb"
NO
I'm learning C by cs50 in edX for a day. Week 1 of cs50 in edX was about making several rows of sharps that the number is escalating by the row increases like this:
#
##
###
####
#####
If I answer "Height=5".
I have no idea how to do it. What can I do in this code?
Here's my code:
// Print escalating numbers of sharp marks
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height:");
}
while (n > 1 && n < 8);
for (int i = 0; i < n; i++)
{
for (int j = 0; j == n; j++)
{
printf("#");
}
printf("\n");
}
}
Your program has two basic problems:
1.
The condition of the do while loop n > 1 && n < 8 is wrong. With that, you iterate the loop as long as n is between 1 and 8. I think you want the opposite - to only iterate if n is not between 1 and 8. Use n < 1 || n > 8 or alternatively !(n > 1 && n < 8) instead.
2.
The inner for loop*s condition is wrong, too. for (int j = 0; j == i; j++) - With that you only print one # per row. You need for (int j = 0; j <= i; j++) instead.
3.
You need to print a newline after the input to correctly format the output of the #s.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
putchar('\n');
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
printf("#");
}
printf("\n");
}
}
Execution:
./a.out
Height: 5
#
##
###
####
#####
Online Test
Technically there is also another sneaky way to achieve when you don't want to have the extra statement to print the newline.
Here we change the outer for loop statement to for (int i = 0; i <= n; i++) and inner to for (int j = 0; j < i; j++).
We use one iteration inside of the outer for loop more and print only the newline in the first walkthrough.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
printf("#");
}
printf("\n");
}
}
Execution:
./a.out
Height: 5
#
##
###
####
#####
Online Test
Edid: Thanks for the suggestions, but none of them work for the FOR loop error.
Why the triple nested for loop(line 82 - 98) works only the first time(correctly) and then it just spits crazy random numbers? The first time it works so good and then it does everything wrong. Thanks for the help!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
// 2 players, 5 zara, 3 puti(ako ne si gi haresa), moje da pazite:
//1 qift
// 2 qifta
// ful: 1*2 i 1*3
// 5 ednakvi: general
// 4 ednakvi
// 1*3
// 5 poredni
// shans
// izpulnqvase 8 puti i moje da se zapazva zara (samo pri 1 i 2 hvurlane)
int np, otg[5], otg10[5];
int check_pairs(int *otg10, int time){
int dicecopy[5];
memcpy(dicecopy, otg10, sizeof(int) * 5);
for(int k = 0; k < 5; k++){
printf("%d " , dicecopy[k]);
}
int pairs = 0, sum = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(dicecopy[i] == dicecopy[j])
pairs++;
}
}
// points
if(pairs == 1*2 || pairs == 2*2){
printf("GGGGGGG");
for(int k = 0; k < 5; k++){
sum += dicecopy[k];
}
} else {
return 0;
}
return sum;
}
int main(){
printf("How many players do you want: ");
scanf("%d" , &np);
int points[np];
for(int i = 0; i < np; i++){
points[i] = 0;
}
int dice[8][np][3][5];
srand(time(NULL));
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
dice[i][j][c][k] = rand() % 6 + 1;
}
}
}
}
/* for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
printf("%d " , dice[i][j][c][k]);
}
putchar('\n');
}
putchar('\n');
}
putchar('\n\n');
}
*/
char anws = 'y';
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
printf("\nPlayer: %d\nPoints: %d\n" , j + 1 , points[j]);
for(int k = 0; k < 3; k++){
for(int l = 0; l < 5; l++){
otg10[l] = dice[i][j][k][l];
printf("%d " , otg10[l]);
}
printf("\n\nDo you like it?\n");
scanf("%s" , &anws);
if(anws == 'y' || anws == 'Y'){
points[j] = check_pairs(otg10 , k);
break;
}
}
}
}
return 0;
}
The code is still not finished I have to do more work on the other check-ups. BTW this will represent the game called Generala, if you have any suggestions I'd like to hear them!
I need to create a program where user will input a number of integers and the program will output the fibonacci sequence number having the next larger number than the user input.
fibonacci sequence as follows:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765
Example:
user inputs: [1, 9, 22]
output should be: 2, 13, 34
I cant seem to make the if loop stop once it has already acquired a value because the output will always display 4181, 4181, 4181.
My code is as follows below:
#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, a[100];
int inputarray[3], j;
int outputarray[3];
a[0] = 0;
a[1] = 1;
for (i = 2; i < 20; i++) {
a[i] = a[i-1] + a[i-2];
}
for (i = 0; i < 20; i++) {
printf("%5d",a[i]);
}
for (j = 0; j < 3; j++) {
printf ("\nEnter numbers of input array:");
scanf ("%d", &inputarray[j]);
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 20; j++) {
if (a[j] > inputarray[i])
outputarray[i] = a[j];
}
}
for (i = 0; i < 3; i++) {
printf("%5d", outputarray[i]);
}
getch();
}
Add break statement because once you found required value, you don't have to keep looping for j
for (i=0; i<3; i++)
{
for (j=0; j<20; j++)
{
if(a[j] > inputarray[i])
{
outputarray[i] = a[j];
break;
}
}
}
I would be much easier if you did not use a for loop (because you don't want to iterate over a given amount of values). If your loop depends on a condition use a do-while/while loop.
for (i = 0; i < 3; i++) {
while (a[j] <= inputarray[i])
{
j++;
}
outputarray[i] = a[j];
}