I have written a code, and it gives me the exact output that I want except for one thing, the alignment of integers in the output is from left to right but I need it to be from right to left.
Here is my code:
#include <stdio.h>
int main()
{
int cols, rows, i,j,num;
scanf("%d %d",&rows, &cols);
for (i=1; i<=1; i++){
for (j=0; j<=cols; j++){
if (j==0) printf("\t ");
else {
num=j;
printf("\t%d", num);
}
}
printf("\n");
}
for (i=2; i<=rows; i++){
for (j=0; j<=cols; j++){
if (j==0) num=i;
else num=power(i,j);
printf("\t%d", num);
}
printf("\n");
}
return 0;
}
The output that I am getting is
The required output is
Use e.g. %6d in the printf() format specifier, and don't use tabs.
See the manual for the format specifier syntax. Field width is a standard feature.
Related
I have a data set with specific format that I want to read in C and also print in certain format. Here is the format of my data in text file:
sample[0][0]=1; sample[0][1]=3;
sample[1][0]=2; sample[1][1]=4;
sample[2][0]=3; sample[2][1]=5;
and I want to read this and return
(*inputs)[0][0] = 1; (*outputs)[0]=3;
(*inputs)[1][0] = 2; (*outputs)[1]=4;
(*inputs)[2][0] = 3; (*outputs)[2]=5;
I was able to write a code that will read my file in just the following format:
1 3
2 4
3 5
and here is my code:
int main() {
FILE *samples;
samples = fopen ("peaks.txt","r");
int arr[10][2];
int i;
int j;
for (i=0; i<10; i++) {
for (j=0; j<2; j++) {
fscanf(samples, "%d, ", &arr[i][j]);
printf("int = %d\t", arr[i][j]);
}
printf("\n");
}
}
How do I modify this code to read the format above? Thank you.
You could read in with, for example for the first line of data,
fscanf(filein, "sample[0][0]=%d; sample[0][1]=%d;", &arr[0][0], &arr[0][1]);
but it is very fragile becaues if the input file is not exactly that then you will get unpredictable behaviour
You could print out with
printf("(*inputs)[0][0] = %d; (*outputs)[0]=%d;", arr[0][0], arr[0][1]);
but probably I got the print statement wrong as I don't know exactly what you want your code to do.
Now for lots of data.....
new version... much simpler...D'oh!
int a,b;
for (i=0; i<10; i++) {
for (j=0; j<2; j++) {
fscanf(samples, "sample[%d][%d]=\%d;", &a, &b, &arr[i][j]);
printf("int = %d\t", arr[i][j]);
}
printf("\n");
}
so now you read in the number values from sample as a and b - but then don't used these value of a and b - though to write a good program you could check the values of a and b are equal to i and j as you expect.
old not working version below
char* string_input[100]; // we need this to get the right format for scanf...
for (i=0; i<10; i++) {
for (j=0; j<2; j++) {
sprintf(string_input, "sample[%d][%d]=\%d;", i, j)
fscanf(samples, string_input, &arr[i][j]);
printf("int = %d\t", arr[i][j]);
}
printf("\n");
}
so what this does is make a taylor made string suitable for reading in your data line by line...
so for example if i=8 and j=1 then string_input will be sample[8][1]=%d;
I want to print all combinations of length 2 from an array of characters (like aa, ab, ..., az, ba, bb, ...., etc). Can somebody explain to me why this code incorrect:
int main(void){
char a[]="abcdefghijk";
for (int i=0; i<11; i++){
for (int j=0; j<11 && a[j]!='\n'; j++){
char r[2] = {a[i], a[j]};
printf("I-th element: %c ", a[i]);
printf("J-th element: %c ", a[j]);
printf("Together: %s", r);
printf("\n");
}
}
return 0;
}
The problem is that after every eleventh combination empty line is printed. If an array is shorter than 10 everything seemed to be fine.
Output looks like this:
screenshot of my IDE
here is a version of the posted code, that does not have any undefined behavior, so the output is as expected.
#include <stdio.h> // printf()
int main(void)
{
char a[]="abcdefghijk";
for (int i=0; i<11; i++)
{
for (int j=0; j<11; j++)
{
printf("I-th element: %c ", a[i]);
printf("J-th element: %c ", a[j]);
printf("Together: %c%c", a[i], a[j]);
printf("\n");
}
}
return 0;
}
I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.
I have the following code:
#include <stdio.h>
int main() {
int tc,T;
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i,j]);
}
}
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i,j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}
Which gives me the following errors:
warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[(sizetype)(C)]'
scanf("%d", &Grid[i,j]);
warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[(sizetype)(c)]'
scanf("%d", &Pattern[i,j]);
Do you know what's wrong with this code?
P.S I also checked the Grid and the Pattern and it reads trash!
In C you should subscript arrays like this: array[i][j].
array[i, j] is equivalent to array[j], since i is ignored.
Arrays in c are not accessed like Grid[i, j] but as Grid[i][j]. So change it to that in your code. Similarly Pattern[i, j] should be written as Pattern[i][j].
This will solve your problem.
Happy to Help! :)
Try something like following:
#include <stdio.h>
int main() {
int tc,T;
printf("Enter one number\n");
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
printf("Enter 2 numbers\n");
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
printf("now reading the grid\n");
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i][j]);
}
}
printf("Enter 2 mre numbers\n");
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
printf("now reading the pattern\n");
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i][j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}
I'm all new to C and just wanted to try some programming for fun! My first idea was to create a Tic-Tac-Toe game. In the following code I'm trying to generate a field. It works to some degree, but when I test it the entries feld[1][0] and feld[2][0] are empty. Also Something I dont understand is, if I save more then one letter in an entry, for example xx, it apears somewhere else. I'm guessings it's a problem with the saveing space assignment of C. Glad for any feedback!
#include <stdio.h>
main()
{
int i,j;
char feld[3][3];
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t %2i. column %2i. row: ", i+1, j+1);
scanf("%s", &feld[i][j]);
}
}
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t %c", feld[i][j]);
}
printf("\n");
}
}
With this line:
scanf("%s", &feld[i][j]);
you are reading a string (multiple characters) and placing them where should only be one character.
This will cause damage to characters that are stored nearby.
Use something like:
scanf("%c", &feld[i][j]);
to read only one character each time around.
But this solution is not perfect either, because now if you feed too many character they will remain stored until you try to read them again, which results in some odd behaviour, like printing multiple times without waiting for your inputs:
2. column 1. row: 3. column 1. row: 1. column 2. row:
The correct answer depends on what you want to happen if you feed multiple inputs at once.
Below is the working code. Each element in the array is a character. %s is used to scan a string not a character. You need to use %c. Add space before %c for scanf to eat/gobble whitespaces and special characters.(like enter)
#include <stdio.h>
main()
{
int i,j;
char feld[3][3];
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t %2i. Row %2i. Column:\n ", j+1, i+1);
scanf(" %c", &feld[j][i]);
}
}
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t%c", feld[j][i]);
}
printf("\n");
}
}
First of all in your code 'j' is representing row so it should be first and i should be after like feld[j][i]
And second in char size is 1 so we can store single char only that is why it problem by use more then one input.
#include <stdio.h>
main()
{
int i,j;
char feld[3][3];
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t %2i. column %2i. row: ", i+1, j+1);
scanf("%c", &feld[j][i]);
}
}
for(j=0; j<3; j++)
{
for(i=0; i<3; i++)
{
printf("\t %c", feld[i][j]);
}
printf("\n");
}
}