Array with chars in c - c

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");
}
}

Related

Array output difficulties

I have been practising with arrays for a bit and I have encountered a problem I can't seem to find the answer for. I am trying to display the numbers the user enters, however they are not turning out as I expected. It should be in a form of a column.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
printf("A = (%f) B = (%f) \n", A[i], B[i]);
return 0;
}
The printf statement seems to be correct however numbers are not showing in the output.
You should ask yourself, what is the value of i, when printing the final output.
You should also ask yourself, what is in array A and B at index i.
Given these are understood, we can display the content of an array in the same fashion as it is filled.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for (i=0; i<=4; i++)
{
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}
As said by #Tsakiroglou Fotis, you forgot to add bracket after the main function and also you are not looping the final print statement to print all the elements. Try using editors that does take care of such mistakes. here is your corrected code
#include <stdio.h>
int main (void){
double A[5], B[5];
int i;
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for(i=0; i<5; i++){
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}

Reading formatted data in C

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;

Using Functions to Pass an Array

Hi, i am using a Function for the first time for my 2-D Array and so far i have been told it is right but i am having a problem with the 'For Loop'.
Specifically repeating the printed statement and numbers until the array is full, and also shows the specific array you are filling. (As the image shows).
This only prints 1 statement with 1 Array, whilst 4 numbers afterwards.. How could i get each number printed after each statement? I have tried the only way i know which made it worse.
Incorrectly Looped printed statement
Code below:
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
You forgot to properly define the scope of the inner for loops.
The lines
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
are equivalent to:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
// Outside of both the for loops.
scanf("%d", &a[i][j]);
The indent expresses the intent to a human reader but not to the computer.
You need to use:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
To make the code more readable add an explicit scope for the outer for loops too.
for (i=0; i<2; i++)
{ // Add
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
} // Add
Make similar changes to the other loops.
Your for loops are probably not acting as you intended, because without the curly braces, they will only act on the first statement.
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
}
}
You're missing some curly braces. Change to this:
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
and
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d \n" , a[i][j]);
if(j==2)
printf("\n");
}
Also, note that if(j==2) will never evaluate to true. When j is equal to 2, the loop is over. You should change this to 1. I would also suggest to remove \n in the innermost printf statement.
Complete code:
#include <stdio.h>
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d " , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==1)
printf("\n");
}
}

Output correct alignment

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.

Sudoku Solver Input

I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.
My primary question is: Why is the method I'm using not placing the user input data into the game board array?
#include <stdio.h>
#include <string.h>
#define CELL 81
int main()
{
// Banner
printf("\t\t\t\tSudoku Solver\n");
printf("\t\t\t***************************\n");
//initialize variables
char Game_Board[9][9];
int i,j;
char Game_Buffer[CELL];
int counter = 0;
printf("Please enter the numbers of the board * denotes a blank space\n");
fgets(Game_Buffer,CELL,stdin);
for(i=0;i<strlen(Game_Buffer);i++)
printf("%c", Game_Buffer[i]);
while(counter < 81)
{
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
printf("%d\n", counter);
printf("\t\t\t\t The Board\n");
for( i=0; i<9; i++)
for( j=0; j<9; j++)
{
if( j % 3 == 0)
printf("|");
printf("%c", Game_Board[i][j]);
if(j==8)
printf("|\n");
}
return 0;
}
You probably should use brackets at first place.
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
}
Add all missing brackets and check if your issue still exists.
The counter++ executes after the loop. I have idented the code to show what I mean..
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
You are updating all cells with the same value.

Resources