My code seems to be working fine, but I get a Segmentation fault when there is just one value as input. It should print a square shape based on a number as character input.
test case : ["2", "2"]
"oo\noo\n"
test case: ["", ""]
""
test case : ["2"]
SIGSEGV (signal 11)
#include <string.h>
#include <stdlib.h>
void my_square(int *x, int *y)
{
int i , j;
if (*x == 0 || *y == 0) {
printf("");
}
else{
for(i = 0; i < *x; i++){
for(j = 0; j < *y; j++){
if(*x<=2 && j == 0){
printf("o");
}else if(*x<=2 && j == 1){
printf("o\n");
}else if(*y<=2 && i == 0){
printf("o");
}else if(*y<=2 && i == 1){
printf("o\n");
}else{
//printf(" i: %d, j: %d ", i, j);
if(i == 0 && j == 0 || i == *y-1 && j == 0 || i == 0 && j == *x-1 || i == *y-1 && j == *x-1){
printf("o");
}
if(i >= 1 && j == 0 && i != *y-1) {
printf("|");
}
if(i >= 1 && j == *x-1 && i != *y-1) {
printf("|");
}
if(i == 0 && j >= 1 && j != *y-1|| i == *x-1 && j >= 1 && j != *y-1){
printf("-");
}
if(i >= 1 && j >= 1 && i < *x-1 && j < *y-1){
printf(" ");
}
if(j == *x-1){
printf("\n");
}
}
//printf("> %d, %d", i, j);
}
}
}
}
int main(int ac, char **av)
{
int x = atoi(av[1]);
int y = atoi(av[2]);
my_square(&x, &y);
return 0;
}```
You should always check ac before accessing av, otherwise it may lead to undefined behaviour (and cause a segmentation fault).
That's how you could do it (the first value is always the program file name):
int main(int ac, char **av)
{
int x, y;
if (ac <= 3)
{
x = atoi(av[1]);
y = x; // if there's only one argument, we use it for both x and y
if (ac == 3)
{
y = atoi(av[2]);
}
my_square(&x, &y);
}
return 0;
}
Related
So I am building a map with borders and filling up with ' * '
Now what I want to do is empty all of the ' * ' and fill them up with blank spaces.
I am not getting the expected output and can't figure out what I am doing wrong, I'd really appreciate if someone could help me.
#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int height=5;
int width=5;
void fill_blank_spaces()
{
gotoxy(0,0);
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{ if(i!=0 && j!=0 && i!=height-1 && j!=width-1)
printf(" ");
}
printf("\n");
}
}
I expect the output to be:
X---X
| |
| |
| |
X---X
But the displayed output is:
X---X
*|
*|
*|
X---X
int main()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf("*") ;
}
printf("\n");
}
fill_blank_spaces();
}
I am new here so excuse my unconventional description.
The top left corner is at ( 1, 1).
The first space on the following lines should go in
( 2, 2), ( 2, 3) and ( 2, 4)
#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int height=5;
int width=5;
void fill_blank_spaces()
{
for ( int i = 1; i < height - 1; i++)
{
for ( int j = 1; j < width - 1; j++)
{
gotoxy ( j + 1, i + 1);
printf ( " ");
}
}
printf ( "\n");
printf ( "\n");
}
int main ( void)
{
gotoxy ( 1, 1);
for ( int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf("*") ;
}
printf("\n");
}
fill_blank_spaces();
}
You don't need fill_blanck_spaces, simply replace * by a space :
int main()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf(" ") ; //<=== Here
}
printf("\n");
}
return 0;
}
I am trying to fill a char matrix in C and print it in C but I get only weird characters. I am running this programm on windows, hence the system(cls);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void paint(char tab[10][10], int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(char tab[10][10], int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes--)
tab[i][j] = 205;
if (j == 0 || j == colonnes--)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes--)
tab[i][j] = 187;
if (i == lignes-- && j == 0)
tab[i][j] = 200;
if (i == lignes-- && j == colonnes--)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
char tab[10][10];
create(tab, 10, 10);
paint(tab, 10, 10);
char i;
while(scanf(" %c", &i) != 'q')
{
}
}
I tried changing the output type in printf with %d and %s as shown in other answers here, but %d shows random numbers and %s makes the programm crash, I don't know if it is because of a segfault somewhere.I also tried using simple characters to fill my matrix, not their ascii value.
Don't mind the ineffective scanf , I am still trying to figure out keyboard input without using enter on my own for now.
there were a few problems with your code, like colonnes-- changes the value of colonnes while cheking your if statement
I believe you wanted to draw a rectangle
try this (it's your own code, modified a bit)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char tab[10][10];
void paint(int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes-1)
tab[i][j] = 205;
if (j == 0 || j == colonnes-1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes-1)
tab[i][j] = 187;
if (i == lignes-1 && j == 0)
tab[i][j] = 200;
if (i == lignes-1 && j == colonnes-1)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
create(10, 10);
paint(10, 10);
}
My code
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i=0;
int j=0;
size_t count=0;
float numbers[20][100];
float velocity[21][101];
char *line = NULL;
FILE *myFile;
myFile = fopen("vel.txt", "r");
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
while(i < 20 && getline(&line, &count, myFile)!=-1) {
int len = 0, pos = 0;
j = 0;
while(j < 100 && 1 == sscanf(line + pos, "%f%n", &numbers[i][j++], &len))
pos += len;
i++;
}
free(line);
fclose(myFile);
i=1;
for( j = 0; j < 101; j++ )
{
if( j == 1 )
{
velocity[i][j]=numbers[i][j];
}
else if ( j == 101 )
{
velocity[i][j]=numbers[i][j];
}
else
{
velocity[i][j]=(numbers[i][j-1]+numbers[i][j])/2;
}
}
for (j=0 ; j<101 ; j++) {
printf("\n%f", velocity[i][j]);
}
}
I need to calculate velocities for 21,101 two dimensional mesh.If i==1 ,that is my code above and works fine.The sam conditions apply if i==21.But for all other values (2 to 20) calculations are different.How should I change
if( i== from 2 to 20 &&j == 1 )
{
do something
}
else if (i== from to to 20 && j == 101 )
{
do something 2
}
else(means i goes from 2,20 j goes from 2,100)
{
do something 3
}
Do you want something like this: if(i >= 2 %% i <= 20)? Means: 2 <= i <= 20 or if i is greater or the same as 2 and i is lower or the same as 20 it is true.
If your example:
if(i >= 2 && i <= 20 && j == 1)
{
//do something
}
else if(i >= 2 && i <= 20 && j == 101)
{
//do something 2
}
else if(i >= 2 && i <= 20 && j >= 2 && j <= 100) //means i goes from 2,20 j goes from 2,100
{
//do something 3
}
or is there anything I missed?
iam new to c program and facing difficulty in debugging programs.In the below code test case 2 is not running.I have found that the error is in reading interger n in the second test case.someone please hep me with this issue.Also please recommend me with some tools that can be ued for debugging c programs using terminal.Thanks for help
#include <stdio.h>
#include <stdlib.h>
int read(){
int r = 0;
char c = getchar_unlocked();
while(c >= '0' && c <= '9'){
r = r*10 + c - 48 ;
c = getchar_unlocked();
}
return r;
}
void main(){
int t = 0;
t = read();
int rr = 0;
for(rr = 0;rr < t;rr++){
int i,n = 0;
n = read();
int *p = (int *)calloc(n,sizeof(int));
for(i = 0;i < n;++i){
*(p+i) = getchar_unlocked() - 48;
}
int no,nz = 0;
for(i = 0;i < n;++i){
if(*(p+i) == 0){nz += 1;}
if(*(p+i) == 1){no += 1;}
}
int k = 0;
if(((no)%2 == 0) && ((nz)%2) == 0){
k = -1;
}
if(((no)%2 == 0) && ((nz)%2) == 1){
k = 0;
}
if(((no)%2 == 1) && ((nz)%2) == 0){
k = 1;
}
if(((no)%2 == 1) && ((nz)%2) == 1){
k = 1;
}
int result = 0;printf("%d\n",5556);
if(k == 1){
for(i = 0;i < n;++i){
if(*(p+i) == 1){
result = i+1 ;
break;
}
}
}
if(k == 0){
for(i = 0;i < n;++i){
if(*(p+i) == 0){
result = i+1 ;
break;
}
}
}
printf("%d\n",result);
}
}
Your strategy to read an integer is flawed. You don't have the logic to skip whitespaces. I would change the function name to read_int and change its implementation to
int read(){
int n;
if ( scanf("%d", &n) != 1 )
{
// Deal with the error
}
return n;
}
Also, change
*(p+i) = getchar_unlocked() - 48;
to
*(p+i) = read_int();
or a more intuitive version:
p[i] = read_int();
With those changes, I am able to read and process the numbers. But I still get the wrong output. I'll let you figure the logic error in your code.
Additional Comments
main is expected to return an int. If your compiler didn't complain about that, it's time to up the warning level. I use -Wall by default.
When you are in the process of debugging your code, it's always good to test the code that reads the input to make sure that there is no error in reading the input.
Here's what I did to your code:
#include <stdio.h>
#include <stdlib.h>
int read_int(){
int n;
if ( scanf("%d", &n) != 1 )
{
// Deal with the error.
}
return n;
}
int main(){
int t = 0;
int rr = 0;
t = read_int();
printf("t = %d\n", t);
for(rr = 0;rr < t;rr++){
int i,n = 0;
n = read_int();
printf("n = %d\n", n);
int *p = (int *)calloc(n,sizeof(int));
for(i = 0;i < n;++i){
p[i] = read_int();
printf("p[%d] = %d\n", i, p[i]);
}
int no,nz = 0;
for(i = 0;i < n;++i){
if(*(p+i) == 0){nz += 1;}
if(*(p+i) == 1){no += 1;}
}
int k = 0;
if(((no)%2 == 0) && ((nz)%2) == 0){
k = -1;
}
if(((no)%2 == 0) && ((nz)%2) == 1){
k = 0;
}
if(((no)%2 == 1) && ((nz)%2) == 0){
k = 1;
}
if(((no)%2 == 1) && ((nz)%2) == 1){
k = 1;
}
int result = 0;
// printf("%d\n",5556);
if(k == 1){
for(i = 0;i < n;++i){
if(*(p+i) == 1){
result = i+1 ;
break;
}
}
}
if(k == 0){
for(i = 0;i < n;++i){
if(*(p+i) == 0){
result = i+1 ;
break;
}
}
}
printf("%d\n",result);
}
return 0;
}
I declare and then assign 'n' to the variables 'm' and 'num'. By the time they reach the end of the program they end up zero.
This program is works to find the Phi Totient function of n. It all works flawlessly until the very last while loop.
int factorization(int n)
{
int i, j=0, a[14], index=0, m, num;
m=n;
num=n;
for(i=2; i<sqrt(n)+1; i++)
{
if(n%i == 0)
{
n=n/i;
if(a[0]!=i && a[1]!=i && a[2]!=i && a[3]!=i && a[4]!=i && a[5]!=i && a[6]!=i && a[! =i && a[8]!=i && a[9]!=i && a[10]!=i && a[11]!=i && a[12]!=i && a[13]!=i && a[14]!=i)
{
a[index]=i;
index++;
}
i=1;
}
}
a[index]=n;
for (i=index+1; i<=14; i++)
{
a[i]=0;
}
for (i=0; i<=10; i++)
{
printf("%d\n",a[i]);
}
while(a[j] != 0 && a[j] != 1)
{
m=m*((a[j]-1)/a[j]);
j++;
}
printf("Phi of %d = %d", num, m);
return 0;
}
Change m to double and loop to
while (a[j] != 0 && a[j] != 1)
{
m = m * ( ((double)a[j] - 1.0) / (double)a[j] );
j++;
}
If m is integer when ever the division
((a[j]-1)/a[j])
is less than 1 it is rounded down to zero and the multiplication becomes
m = m * 0
int i, j=0, a[14], index=0, m, num;
a array is not initialized but the array elements are read in:
if(a[0]!=i && a[1]!=i && a[2]!=i && a[3]!=i && a[4]!=i && a[5]!=i && a[6]!=i && a[! =i && a[8]!=i && a[9]!=i && a[10]!=i && a[11]!=i && a[12]!=i && a[13]!=i && a[14]!=i)
Morever in the above line you are testing a[14]!=i but the last element of a is a[13]. a[14] is out of the array.
Same in:
for (i=index+1; i<=14; i++)
{
a[i]=0;
}
You are accessing an element (a[14]) outside the array.