I'm creating a dynamic 2d character array in C:
Note: rows and columns are user input integers
char** items;
items = (char**)malloc(rows * sizeof(char*));
int i;
for(i = 0; i < rows; i++)
{
items[i] = (char*)malloc(columns * sizeof(char));
}
int j;
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
items[i][j] = 'O';
}
}
Later in my code, I attempt to overwrite a specific location in the array:
items[arbitraryRow][arbitraryColumn] = 'S';
But the result is that the characters in that row/column are now 'SO'
What am I doing wrong?
Update:
This is how I'm printing the array:
int i;
for(i = 0; i < rows; i++)
{
printf("[");
int j;
for(j = 0; j < columns; j++)
{
printf("'%s'", &items[i][j]);
if(j != columns - 1)
printf(", ");
}
printf("]");
printf("\n");
}
You're not storing strings you're storing characters so all you can read is one character so that'd be the S
My suspision is that the next character is an O so when you look at it as a string you get SO
printf("'%c'", items[i][j]);
You are storing characters and reading strings. Try reading character back from the Array.
Change your code to:
int i;
for(i = 0; i < rows; i++)
{
printf("[");
int j;
for(j = 0; j < columns; j++)
{
printf("'%c'", items[i][j]);
if(j != columns - 1)
printf(", ");
}
printf("]");
printf("\n");
}
Related
I am trying to add numbers from 0 to 9 in the row and 0 to 11 in column of a two dimensional array in C. And as for the rest of the empty spaces, I would like to add 0.
The matrix size is 9 x 11. And this is what the output looks like with the empty blocks filled with 0:
And this is the code I have so far but it does not work:
int i;
int j;
int arr[i][j];
int value = 0;
for (i = 0; i < 9; i++){
for (j = 0; j < 11; j++){
arr[i][j] = value;
printf("%d\n", arr[i][j]);
value++;
}
printf("\n");
}
The screenshot that you've posted has 10 rows and 12 columns, so assuming that, here is the code:
int i;
int j;
int arr[10][12];
for (i = 0; i < 10; i++) {
for (j = 0; j < 12; j++) {
if (i == 0) {
arr[0][j] = j;
} else if (j == 0) {
arr[i][0] = i;
} else {
arr[i][j] = 0;
}
printf("%d", arr[i][j]);
}
printf("\n");
}
i cannot find solution how to solve this.
rows number is easy one. reading from keyboard and allocation but column number is different for each row.
lets assume user entered 2 rows after that user entered 3 column for row 0 and 10 column for row 1
and i dont know how to enter values for these columns and print them. because every column has different length. unfortunately != NULL not working
int** ptr; int rows, columns;
printf("enter number of rows..\n");
scanf("%d", &rows);
ptr = (int**)calloc(rows, sizeof(int*));
for (size_t i = 0; i < rows; i++) // allocation an array of integers for every row
{
printf("enter number of columns..\n");
scanf("%d", &columns);
ptr[i] = (int*)calloc(columns, sizeof(int));
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != NULL; j++)
ptr[i][j] = j;
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != NULL; j++)
printf("%d\n", ptr[i][j]);
}
Since C arrays don't have a final delimiter, then you need to store the length in an efficient way:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int** ptr; int rows, columns;
printf("enter number of rows..\n");
scanf("%d", &rows);
ptr = (int**)calloc(rows, sizeof(int*));
for (size_t i = 0; i < rows; i++) // allocation an array of integers for every row
{
printf("enter number of columns..\n");
scanf("%d", &columns);
ptr[i] = (int*)calloc(columns+1, sizeof(int)); // Last pos will save the size
// We need to track the columns since C arrays do not have a final delimiter
ptr[i][columns] = INT_MIN; // It could also be INT_MAX depending on our needs
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != INT_MIN; j++)
ptr[i][j] = j;
}
for (size_t i = 0; i < rows; i++)
{
printf("Line %li\n", i);
for (size_t j = 0; ptr[i][j] != INT_MIN; j++)
{
printf(" Column %li: ", j);
printf("%d\n", ptr[i][j]);
}
}
return 0;
}
I am writing a program which determines the intersection of 2 integer arrays (size of 10 elements). I think I got every other parts covered except for sorting out duplicates. Does anyone know a way of checking duplicates without making a function or using an external C library?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '\0';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '\0')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
In the code above i was trying one method although it didn't work and instead made the program stuck after inputting all the elements. I am open to other methods as long it doesn't require an external library to run. Thanks
As i see it now , in the third loop where you checking your duplicates i thing that you have to increese k not i :
for (int k = j; j < count; k++), also you must decrise the size of j in your code under the count--;.So your code for checking duplicates seems right but , you want the intersection of this two arrays you made , so you dont have to check for duplicates because in the array intersection[SIZE] you will put only one number from the two arrays, so you will not have duplicates .You should check for duplicates if you wanted to make the union of this two arrays .I make some changings to your code acording what you want to create and this code here find the intersection from two arrays.Try this and delete the duplicate check because that makes your code to run to infinity . One last thing your intersection check must be replace whith this :
//Intersection check
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);
I'm working on taking 10 numbers as user input and having the input printed in two columns with one sorted column.
#include <stdio.h>
int main(void)
{
int NUM_ELEMENTS = 10;
int list[NUM_ELEMENTS];
int sortedList[NUM_ELEMENTS];
int i, j, temp;
printf("Enter 10 numbers:\n");
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];}
}
}
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) {
temp = list[i];
sortedList[j] = temp;
}
}
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]);
}
return 0;
}
The first column prints correctly but the second column doesn't print the correct numbers. I'm fairly new at coding in C and can't seem to get this to work right. I've tried several variations of the provided code. The columns are also supposed to be side by side and labeled as well.
There are lot of bugs in the above code.
-firstly, looping constraints are wrong in scanning and assigning part
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];} /* for i=0 why list[0] assigned to all sortedList[j] ?
}
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d", &list[i]);
sortedList[i] = list[i];
}
-Secondly,sorting logic is not correct at all.
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) { /* what is sortedList[j] ? */
temp = list[i];
sortedList[j] = temp;
}
}
Replace above one with simple sorting technique logic for now but once become strong with basics check performance view also, which sorting method is better to use.
for(i = 0; i < NUM_ELEMENTS-1; i++) {
for(j = 0;j < NUM_ELEMENTS-1-i; j++) {
if(sortedList[j] > sortedList[j+1]) {
temp = sortedList[j];
sortedList[j] = sortedList[j+1];/* you miss this */
sortedList[j+1] = temp;
}
}
}
-Finally, there is bug in print part also.
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]); /* what is j here ? you can use the same variable i for both. */
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[i]);
}
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();
}