I want to create the following looking 2D array of "-"
I attempted something like this but it did not work (for 10 by 10)
char table[10][10];
for (int i = 0; i < 10; i ++){
for (int j = 0; j < 10; j++)
{
strcpy(table[10][i], "-");
}
}
The whole 2D array?
If not strings, use memset(table, '-', sizeof table); to fill every byte with '-'. No for loop needed.
char table[10][10];
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ++){
for (size_t j = 0; j < sizeof(table[i]); j++)
{
table[i][j] = `-`);
}
}
or memset(table, '-', sizeof(table))
If you want to have 10 strings (null character terminated)
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ++){
memset(table[i], `-`, sizeof(table[i]) - 1);
table[i][sizeof(table[i]) - 1] = 0;
}
Not advocating this for portability, but if you're using GCC, you can initialize at the declaration using the following GNU extension
char table[10][10] = { [0 ... 9] = {[0 ... 9] = '-'} };
Demo
(blatant ripoff of this answer)
Instead of using strcpy we can assign values like this // table[i][j]= '-'; //
printf("%c",table[i][j]); //It is used to print characters//
#include <stdio.h>
int main()
{
char table[10][10];
for (int i = 0; i < 10; i ++)
{
for (int j = 0; j < 10; j++)
{
table[i][j]= '-'; //instead of using strcpy we can assign values like this//
}
}
for (int i = 0; i < 10; i ++)
{
for (int j = 0; j < 10; j++)
{
printf("%c",table[i][j]); //It is used to print characters//
}
printf("\n"); //It is used to have a new line//
}
return 0;
}
Related
I spent hours in printing a lower triangle in C. However, I just cannot figure out how to solve this same question with array.
Below is one of the solution I found on net:
int main(void)
{
char ch='A';
int i,j;
for(i=1;i<7;i++)
{
for(j=0;j<i;j++)
printf("%c",ch++);
printf("\n");
}
return 0;
}
Below is how I try to do the same thing:
#define SIZE 8
int main(void){
char Alphabet[SIZE];
int i, j;
for (i = 0, j = 'A'; i < SIZE, j < 'A' + SIZE; i++, j++){
Alphabet[i] = j;
}
for (i = 0; i <= 7; i++){
for (j = 0; j <= i; j++){
printf("%c", Alphabet[j+i]);
}
printf("\n");
}
return 0;
}
The result of the code above is :
A
BC
CDE
DEFG
EFGHI
FGHIJK
GHIJKLM
HIJKLMNO
What should I revise if I want to print as follow:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
Thank you.
Keep a track of elements printed from the Alphabet array so far and in the inner loop start printing from next element onward. You can do:
#include <stdio.h>
#define SIZE 26
int main(void) {
char Alphabet[SIZE];
for (int i = 0; i < SIZE; i++) {
Alphabet[i] = 'A' + i;
}
// Or simply have the Alphabet array initialized like this
// char Alphabet[SIZE] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int k = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j <= i && k < SIZE; j++) {
printf("%c", Alphabet[k++]);
}
printf("\n");
}
return 0;
}
Output:
# ./a.out
A
BC
DEF
GHIJ
KLMNO
PQRSTU
EDIT:
In the comments, a fellow SO contributor said that the above approach is same as the one OP already found as a solution and OP might be looking for approach of calculating the Alphabet array index using i and j only and without use of variable keeping track of array index. Below is the program which does not use any extra variable to keep the track of Alphabet array index to print characters in inner loop and calculating the index using i and j:
#include <stdio.h>
#define SIZE 26
#define ARRLOC(x) ((x * ((x + 1) / 2)) + ((x % 2 == 0) ? (x / 2) : 0))
int main(void) {
char Alphabet[SIZE];
for (int i = 0; i < SIZE; i++){
Alphabet[i] = 'A' + i;
}
// Or simply have the Alphabet array declared like this
// char Alphabet[SIZE] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 6; i++){
for (int j = 0; j <= i && (ARRLOC(i) + j) < SIZE; j++){
printf("%c", Alphabet[ARRLOC(i) + j]);
}
printf("\n");
}
return 0;
}
Output:
# ./a.out
A
BC
DEF
GHIJ
KLMNO
PQRSTU
You can just have a third 'index' variable that keeps track of which letter to output across both loops (I've called this k in the code below). Also, you need to make your Alphabet array bigger (26 seems like a reasonable number); then, if that k variable gets past 'Z', we can simply loop back to 'A' using the modulo operator (%):
#include <stdio.h>
#define SIZE 26
int main(void)
{
char Alphabet[SIZE];
int i, j, k;
for (i = 0; i < SIZE; i++) Alphabet[i] = 'A' + i;
int k = 0;
for (i = 0; i <= 7; i++) {
for (j = 0; j <= i; j++) {
printf("%c", Alphabet[k % 26]); // If past the end, loop back with the "%" operator
++k;
}
printf("\n");
}
return 0;
}
Or we can make the code a little more 'succinct' (though perhaps less clear) by initializing the k variable at the start of the outer loop and incrementing at the end of the inner loop:
for (k = i = 0; i <= 7; i++) { // Initialize "k" here ...
for (j = 0; j <= i; j++, k++) { // .. but increment it here!
printf("%c", Alphabet[k % 26]); // If past the end, loop back with the "%" operator
}
printf("\n");
}
If you want an 8 by 8 pyramid, you won't have enough characters to do it using the alphabet (requires 36), so I made the alphabet repeat itself (u could also make it go to numeric instead?)
#define SIZE 8
int area(int size);
int main(void){
char Alphabet[area(SIZE)];
int i, j;
for (i = 0, j = 'A'; i < area(SIZE); i++, j++){
if (j > 'Z') j = 'A';
Alphabet[i] = j;
}
int idx=0;
for (i = 0; i < SIZE; i++){
for (j = 0; j <= i; j++){
printf("%c", Alphabet[idx++]);
}
printf("\n");
}
return 0;
}
int area(int size) {
if (size==1) return 1;
return size + area(size - 1);
}
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 found out about that little trick to make this more long-form code (not the i < strlen)...
for (int i = 0; i < strlen(string1); i++)
{
printf("%c", string1[i]);
}
... into this more short-hand code (note the n variable):
for (int i = 0, n = strlen(string1); i < n; i++)
{
printf("%c", string1[i]);
}
my question is, why is NOT "size_t n = strlen(string1)"? Why do you not give n a data type?
Thankyou!
char **array;
char *x_ptr = &array[0][0];
int rowcount = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (j == 0) {
rowcount += 1;
*(x_ptr +( i*column + j)) = rowcount+'0';
}
else {
*(x_ptr +( i*column + j)) = 0;
}
}
}
When running this loop for the for the 10th time, why does it store the int value for 10 as symbol:
Current result
8,9,:,;,<,=,>
The ASCII for '0' is 48. If you add 10 to it, you'll get 58, which is the ASCII for ':'.
You should use char array[10][10]; not char **array;
':' == '9' + 1
The following code could work:
#include <stdio.h>
int main()
{
int row = 10, column = 10;
char array[10][10];
int rowcount = 0;
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j)
if (j == 0)
array[i][j] = ++rowcount + '0';
else
array[i][j] = 0;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j)
printf("%c\t", array[i][j]);
printf("\n");
}
return 0;
}
I would like to fill 2d array, and this is what I do. However, it would give compile errors such as warning: value computed is not used and i dont understand why. I would appreciate if anyone could show me the problem and explain what could be done. thanks!
#include <stdio.h>
#include <string.h>
int main()
{
int array1[4][4];
int len = sizeof(array1) / sizeof(array1[0]);
int wid = sizeof(array1[0]) / sizeof(array1[0][0]);
int i, j , z = 0;
//compile error
for(i = 0, i < len; i++)
{
for(j = 0, j < wid; j++)
{
array1[i][j] = z;
}
z++;
}
int a, b;
for(a = 0, a < len; a++)
{
for(b = 0, b < wid; b++)
{
printf("%d", array1[a][b]);
}
}
return 0;
}
You have put a comma after the initialization part of each of your for statements. You should put a semicolon. For example, you wrote this:
for(i = 0, i < len; i++)
You need to change it to this:
for(i = 0; i < len; i++)
Also, you will probably want to print spaces between array elements, and a newline after each row:
for(a = 0; a < len; a++) {
for(b = 0; b < wid; b++) {
printf("%d ", array1[a][b]);
}
printf("\n");
}