I'm a beginner in C, I'm facing a problem in order to implement a diamond, I'm following guided exercises from a book but I got stuck, I have to implement the pattern:
According to the exercise, they suggest me to implement the pattern using the operator %
I coded the structure but when I want to implement the pattern #.o.# I just destroy everything
#include<stdio.h>
int main(){
int n, space;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("#");
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("#");
}
printf("\n");
space++;
}
}
I really appreciate it if somebody could give me a hint or help with this, I got stuck here for the last 2 weeks.
Thank you.
This is what you need
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
}
The output for some cases:
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
./test
Number of sides: 5
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
./test
Number of sides: 2
#
#.#
For a diamond:
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
space = 0;
for (int i = n-1; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
position=0;
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space++;
}
}
And the output
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#
#.o.o.#
#.o.#
#.#
#
you have to just change printf("#") to printf("%c",pattern[j%4])
Here is a code:
int main(){
int n, space;
char pattern[4] = {'#','.','o','.'};
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space++;
}
}
The reason why you "destroy everything" is probably because you tried to do it with printf(pattern[j%4]). printf as first parameter wants string (address in memory when string is stored) when you pass pattern[j%4], it thinks that for example '#' (in ASCII 64) is adress in memory, and want to read from it, but it is not valid memory adress so operating system kill your program and it "get's destroyed"
Related
The program is to accept a matrix and an integer and replace all the elements with asterisk('*') other than the elements present in upper left and right triangle and lower right and left triangle of size k.
code:
#include <stdio.h>
int main()
{
int n, x, i, j;
// Inputs
printf("n: ");
scanf("%d\n", &n);
char a[n][n], b[n][n];
printf("Enter the matrix elements\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%c\n", &a[i][j]);
b[i][j] = '*';
}
}
printf("x: ");
scanf("%d\n", &x);
printf("x: %d\n",x);
// Implementation
// Top right and left triangles
for (i = 0; i < n; i++)
{
for (j = 0; j < x - i; j++)
{
b[i][j] = a[i][j];
}
for (j = n - 1; j >= (n - x) + i; j--)
{
b[i][j] = a[i][j];
}
}
// Bottom right and left triangles
int c = 0;
for (i = n - 1; i >= (n - x); i--)
{
for (j = 0; j < x - c; j++)
{
b[i][j] = a[i][j];
}
for (j = n - 1; j >= (n-x) + c; j--)
{
b[i][j] = a[i][j];
}
c++;
}
// Printing the mat
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%c ", b[i][j]);
}
printf("\n");
}
return 0;
}
The program works for inputs 1 through 8... I didn't try for inputs past 8 because its was tedious enough to type 64 elements, but when I submitted the program it showed that for test case of matrix size 50 the result was an error, specifically 'utf-8' codec can't decode byte 0x87 in position 377: invalid start byte
The program works fine for inputs 1 to 8. Any idea why this is happening?
My inputs repeat themselves rather then starting from blank and letting me input the next integer. An additional issue is that I am unable to print the matrix like "100 200" and then "300 400" on the next line. I have tried messing with the newlines and taking out the scan function but nothing seems to work.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
printf("%d",mat[i][j]);
}
}
printf("Enter Matrix B\n");
int matb[100][100];
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
printf("%d",matb[i][j]);
}
}
printf("A+B =\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j){
int new;
new = mat[i][j]+matb[i][j];
printf("%d",new);
}
}
}
When you're reprinting the numbers after the user enters them, end with a newline so they don't enter the next number on the same line.
When you're printing the sums, put a space after each number, and a newline after each row.
I've annotated the changes below with comments.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
printf("%d\n",mat[i][j]); // added newline
}
}
printf("Enter Matrix B\n");
int matb[100][100];
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
printf("%d\n",matb[i][j]); // added newline
}
}
printf("A+B =\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j){
int new;
new = mat[i][j]+matb[i][j];
printf("%d ",new); // added space
}
printf("\n"); // added newline
}
}
I was looking through your code and saw the following conceptual errors:
You do declare i,j at the main beginning but also declared locals for each for loop, that’s not necessarily you can choose one way or another,
You cant obtain a printed results by rows because you are printing each value since you have the print statement nested inside both loops, for row printing you have to take out the print of the second loop. Also you have to print newline like was pointed in the other answer.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
}
printf("\n");
}
printf("Enter Matrix B\n");
int matb[100][100];
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
}
printf("\n");
}
printf("A+B =\n");
int new;
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j){
new = mat[i][j]+matb[i][j];
printf("%d ", new);
}
printf("\n");
}
}
That's it
Output:
> cc test.c -o test.exe && test
Please enter the number of rows: 2
Please enter the number of columns: 2
Enter Matrix A
10 20
30 40
Enter Matrix B
1 2
3 4
A+B =
11 22
33 44
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;
}
Why is there an error in these two codes which should be the same?
This one works:
int N=4;
int M[N][N];
for(int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &M[i][j]);
}
}
This one only reads one line:
int i=0,j=0,N=4;
int M[N][N];
for(i; i < N; i++) {
for (j; j < N; j++) {
scanf("%d", &M[i][j]);
}
}
But if you add space in front of %d in scanf function it will read two rows.
You are not re-initializing j to zero on each iteration, you need:
int i=0,j=0,N=4;
int M[N][N];
for(i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
scanf("%d", &M[i][j]);
}
}
int i=0,j=0,N=4;
int M[N][N];
//for(i; i < N; i++) {
//for (j; j < N; j++) {
scanf("%d", &M[i][j]); // i = 0; j = 0
//}
//}
j++;
scanf("%d", &M[i][j]); // i = 0; j = 1
j++;
scanf("%d", &M[i][j]); // i = 0; j = 2
j++;
scanf("%d", &M[i][j]); // i = 0; j = 3
i++;
scanf("%d", &M[i][j]); // i = 1; j = 3
i++;
scanf("%d", &M[i][j]); // i = 2; j = 3
I am trying to copy elements from two arrays into a third.
I can't understand why it doesn't work.
I made sure that the two arrays are filled properly, but for some reason, the actual copying doesn't work- when I print the elements of the arr3, I get some random numbers.
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n;
printf("Enter a number of elements to be stored in each array (up to 10): ");
scanf("%d", &n);
printf("Enter the %d elements to the first array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
printf("Enter the %d elements to the second array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
/*
// A test to make sure first 2 array are filled by the user- works
for(i = 0; i < n; i++)
printf("%d ", arr1[i]);
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
*/
// something wrong here, the elements are not coppied to the third array
for(i = 0; i < n; i++);
arr3[i] = arr1[i];
for(i = n; i < 2 * n; i++)
arr3[i] = arr2[i];
for(i = 0; i < 2 * n; i++)
printf("%d\n", arr3[i]);
return(0);
}
You're reading past the end of arr2, try this;
for (i = 0; i < n; i++)
arr3[i] = arr1[i];
for (i = 0; i < n; i++)
arr3[n+i] = arr2[i];
That's because your code is reading arr2[10],arr2[11] ..... arr2[19] (if n=10 ), all these values do not exist because arr2 only has 10 values. you can use this.
for (i=0; i<n; i++)
arr3[n+i]=arr2[i];
or
for (i=n; i<n*2; i++)
arr3[i]=arr2[i-n];