I have been searching all google or stackoverflow, but could not found it. :(
I have 100 strings, each string is a number with length = 100, the strings are seperated by a break_line.
Example of input:
010011001100..... (100 numbers)
...(98 strings)
0011010101010.... (100 numbers)
the ouput should be an array A[100][100] for each single number from the strings.
My codes do not work, could you please help to correct it:
#include <stdio.h>
char a[100][100];
int b[100][100];
int i,j;
int main(void)
{
for(i = 0; i < 100; i ++){
for(j = 0; j < 100; j ++){
scanf ("%s", a[i][j]);
b[i][j] = a[i][j] - '0';
printf("%d", b[i][j]);
}
printf("\n");
}
}
Thank you so much. !
Your code has two problems:
#include <stdio.h>
char a[100][100]; /* No space for the NUL-terminator */
int b[100][100];
int i,j;
int main(void)
{
for(i = 0; i < 100; i ++){
for(j = 0; j < 100; j ++){
scanf ("%s", a[i][j]); /* %s expects a char*, not a char */
b[i][j] = a[i][j] - '0';
printf("%d", b[i][j]);
}
printf("\n");
}
}
It should be
#include <stdio.h>
char a[100][101]; /* Note the 101 instead of 100 */
int b[100][100];
int i,j;
int main(void)
{
for(i = 0; i < 100; i ++){
scanf ("%s", a[i]); /* Scan a string */
for(j = 0; j < 100; j++){
b[i][j] = a[i][j] - '0';
printf("%d", b[i][j]);
}
printf("\n");
}
}
or
#include <stdio.h>
char a[100][100]; /* No need for space for the NUL-terminator as %s isn't used */
int b[100][100];
int i,j;
int main(void)
{
for(i = 0; i < 100; i ++){
for(j = 0; j < 100; j ++){
scanf (" %c", &a[i][j]); /* Scan one character, space before %c ignores whitespace characters like '\n' */
b[i][j] = a[i][j] - '0';
printf("%d", b[i][j]);
}
printf("\n");
}
}
I got the answer for my problem from Mr./Ms. BLUEPIXY.
It is
scanf("%1d", &b[i][j]);
Related
Newb Here!
Trying some things with C Language,
I tried to print Half left triangle/pyramid and Half right triangle/pyramid together in one print.
heres the code!
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i) // outer loop
{
for (j = 1; j <= i; ++j) // inner loop
{
printf ("*"); // print the Star
}
printf ("\n");
}
{
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*"); // print the Star
}
printf ("\n");
}
}
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
int hollow_spacing = 2;
int left_padding = 10;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 0; i < rows; i++)
{
int curr_left_pad = left_padding + rows - i;
while(curr_left_pad-- > 0){
printf (" ");
}
for (int j = i; j >= 0; j--)
printf ("*");
for (int j = 0; j < hollow_spacing; ++j)
printf (" ");
for (int j = i; j >= 0; j--)
printf ("*");
printf ("\n");
}
}
I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;
I am trying to create a program which prints a matrix of integers, but the output returns weird numbers before the actual matrix. There are no compiling errors.
This is what my code looks like: //ignore void function for now, focus on main function::
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int array[30][30], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d", array[i][j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i][j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i][j]);
}
printf("\n");
}
return 0;
}
And this is my output (I only want the last three lines)
123
-514159984327663
-51415932632766-514159305
1 2 3
4 5 6
7 8 9
You are missing a space in "%d" in printmatrix, and, more importantly, it is not proper to pass an int [n][n] array for an int [30][30] parameter unless n is 30.
Change void printmatrix(int array[30][30], int n) to void printmatrix(int n, int array[n][n]), and change printmatrix(ints2D, n); to printmatrix(n, ints2D);. That makes the type of the argument you are passing match the type of the parameter.
In your function args you defined the array as fixed size ([30][30]) but you are passing a VLA ([3][3] in your example) which makes it find uninitialized memory and why you are seeing the strange numbers.
#Eric Postpischil answer is spot on. Another way to solve it: 2d arrays could be flatten into 1d. Here is a working code for you:
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int *array, int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i * n + j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n * n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i * n + j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i * n + j]);
}
printf("\n");
}
return 0;
}
How do I print the elements of a 2D Char Array in C?
Here is my current code:
int main()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d",&size);
char word[size][size];
//Enter the matrix
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%c",&word[k][j]);
}
}
//printf("\n");
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
//printf("\n ");
}
printf("\n");
}
When executed it returns the element in pairs (using a 4x4 array)
Example:
ab
cd
ef
gh
ij
kl
mn
op
Rather than my desired output:
abcd
efgh
ijkl
mnop
Why is this?
changing your scanf solves all the problems
scanf(" %c",&word[k][j]); // notice the space before '%c'
And also you need to change your printing loop to this
for (k = 0; k < size; ++k){
for(j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n");
}
Beware: %c and %1s do different things (apart from adding a terminating null for the latter):
c take every character including space, tab, cr and lf
%1s skip over all blanks (space, tab, cr, lf, etc.)
So at input time, you should use:
char c[2]; // provide room for a terminating null...
...
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%1s",c);
word[k][j] = c[0];
}
}
And at print time:
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n "); // new line after each line
}
I removed the reading and it seems like printing is ok:
int main()
{
const unsigned int size = 4;
char word[size][size];
//Enter the matrix
for (int k = 0; k < (size); ++k) {
for (int j = 0; j < (size); ++j) {
word[k][j] = 'a' + j + (k * size);
}
}
for (int k = 0; k < size; ++k) {
for (int j = 0; j < size; ++j) {
printf("%c", word[k][j]);
}
printf("\n");
}
printf("\n");
getchar();
return 0;
}
And the output:
abcd
efgh
ijkl
mnop
I found two issues with your source.
One is the memory allocation - that is actually not ansi-c.
If you need dynamic memory you need to allocate it at runtime. Consider switching to c++ since there are standard facilities that help with that in a safer way.
The second issue was that there is a whitespace character in the buffer that is used as an input character. I think you want to clear that.
Here is the source with additional comments:
#include <stdio.h>
#include <stdlib.h>
void ansiC()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d", &size);
//char word[size][size]; <- this is not ansi-c because size is unknown at compile time
char * word = (char*)malloc(sizeof(char)* size * size);
//Enter the matrix
for (int k = 0; k < (size); ++k)
{
for (int j = 0; j < (size); ++j)
{
printf("Enter letter:");
scanf("%c ", &word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
//after the character the user presses the enter key
//this puts a whitespace character on the buffer.
//by adding the space after %c you also clear that from the buffer
}
}
//printf("\n");
for (int k = 0; k < size; ++k)
{
for (int j = 0; j < size; ++j)
{
printf("%c", word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
}
//printf("\n ");
}
printf("\n");
free(word); //if you use malloc you need to remember to use free
}
int main()
{
ansiC();
return 0;
}
Please check this .
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
char arr[5][3]={"abc","aks","tny","dkn","kbf"};
for(int a=0;a<5;a++)
{
for(int b=0;b<3;b++)
{
cout<<" "<<arr[a][b];
}
cout<<endl;
}
getch();
}
I am trying to send a pointer of a matrix to function for printing the values. However, my following code prints some long numbers so I assumed it prints the addresses instead! How can I print the value of the matrix after passing the pointer of it?
#include <stdio.h>
#include <string.h>
#include <math.h>
void printing(int *edge);
void main(){
int N=3;
int i,j;
int *edge[N];
for (i = 0; i < N; i++){
*(edge+i) = (int *)malloc(N * sizeof(int));
}
srand(0);
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
if(i == j)
*(*(edge+i)+j) = 0;
else
*(*(edge+i)+j) = 1; //rand() % 10;
}
}
printing(edge); // Pass the pointer of the matrix
}
void printing(int *edge){
int i,j;
int N= 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", ((edge+i)+j)); // I think I have to do something in this line.
}
printf("\n");
}
}
The parameter type of printing is incorrect. It should be int *edge[]. Then when you print, use *(*(edge+i)+j), or better yet edge[i][j].
The result:
void printing(int *edge[]){
int i,j;
int N = 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", edge[i][j]);
}
printf("\n");
}
}
Also, be sure to #include <stdlib.h>, as it's needed for malloc and srand.