Is it possible to display array in C? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to make some batleships, but I don't know if it is posible to display a 2D array for the playing ground?

char arr[SIZE][SIZE];
int i,j;
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
arr[i][j] = 'O'; //initalizes
printf(" %c ",arr[i][j]); //prints
}
purchar('\n'); //to break every row
}
update spot to X when it has been hit, and print again w/o the initializer line
sorry the code came out weird, but it's basically a nested for loop, each counting to predefined size of game board, can be indexed from 0,SIZE-1
maybe make it a char array and use O and X and then other characters to draw the ships put out, honestly i'd make the ships eight, equals equals and a capital D, but the choice is yours

You're probably looking for a for loop. They usually took something like this:
size_t i; // size_t is an unsigned int large enough to hold a string's length
for(i = 0; i < size_of_my_array; ++i) {
// do stuff with my_array[i]
}
For example, to print the characters in a string individually (not necessarily the most efficient way):
char* name = "Brendan";
size_t i; // size_t is an unsigned int large enough to hold a string's length
for(i = 0; i < strlen(name); ++i) {
printf("%c", name[i]);
}
Looping over other kinds of arrays is similar.

Related

Sorting an array - C language [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.

Create Integer List of Lists in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to create an integer list in C containing integer arrays of variable size. The length of the main list will not need to change.
How would I declare it - my implementation - particularly my access is not working:
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
.
.
.
populate
for(int a = 0; a<sizeof(dataArray); a++) {
tempArr = dataArray[a];
for(into = 0; b>sizeof(tempArr); b++) {
print(dataArray[a][b])
}
}
Your main array needs to hold pointers to other arrays. Therefore it should not be int dataArray[length] but rather int* dataArray[length] this means it will hold length amount of references to integer arrays.
int* array[length];
int randomSizeArray[x];
randomSizeArray[0] = 1;
.
.
.
randomSizeArray[x] = 5;
int* array[0] = randomSizeArray;
Also sizeof() will not work the way you expect it to - in C you need to store separately how many elements are in an array. I'd recommend reading a C tutorial from the ground up as you seem to have shaky knowledge of basics.
The sizeof results in the byte count not element count.
Divide the array size by the element size to find the element count.
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
...
// for(int a = 0; a<sizeof(dataArray); a++) {
for(size_t a = 0; a<sizeof(dataArray)/sizeof(dataArray[0]); a++) {
tempArr = dataArray[a];
// for(into = 0; b>sizeof(tempArr); b++) {
for(into = 0; b>sizeof(tempArr)/sizeof(tempArr[0]); b++) {
// or do you really want
// for(size_t = 0; b<sizeof(tempArr)/sizeof(tempArr[0]); b++) {
print(dataArray[a][b])
}
}

How to convert(not just output) elements of an array into it's hexadecimal values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have to implement Rijndael algorithm in C. I start with this:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i=0,j;
char m[5000];
char message[5000];
char ch;
printf("Introduce the message\n\n");
while((ch=getchar())!='\n')
{
message[i]=ch;
i++;
}
message[i]='\0';
i=0;
while(message[i]!='\0')
{
sscanf(&message[i],"%x",&m);
i++;
}
printf("\nResult\n");
for(j=0;j<i;j++)
{
printf(" %x",&m[j]);
}
printf("\n");
}
I need an array in which for example "Hello"(where array1[0] will show H) will be written as 48656c6c6f, and when calling array2[0] it will show 48.
Do you want a hexadecimal representation of message's contents? If so, what you need is this:
char messageHex[sizeof(message)*2];
memset(messageHex, 0, sizeof(messageHex));
size_t len = strlen(message);
for (size_t i = 0; i < len; i++)
{
sprintf(messageHex + i*2, "%02X", message[i] & 0xFF);
}
What use would it be to you to make an array that contains the hexadecimal value in a decimal representation? It's the same numeric value after all.
Printing it would be as simple as printf("%x\n", arr[0]);.
If you insist, then you can have an array of strings (char*) with each containing the string of the hexadecimal value then you might want to use sprintf() (so you won't need to do any calculations yourself) as following:
unsigned char** hex_arr = malloc(arrLen); //arrLen is the integer array length
for(i = 0 ; i < arrLen ; i++) //declare i first
{
hex_arr[i] = malloc(3); //2 characters and null (two hexadecimal notes can represent up to 255 which is the size of the wide ASCII table)
sprintf(hex_arr[i], "%x", (uint8_t)arr[i]); //as arr is your integer array
}
Note that you can also organize everything unto one string (char*) where every character in spot of i in the integer array would be represented by two characters: xArr[i*2] and xArr[i*2+1]
Cheers.

list characters in arrays without duplicate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Well, my question is pretty simple, yet i've been stuck for quite a while. I'd like to write a program that takes two arrays as arguments, and then write the letters in it without duplicates, in order of appearance.
example :
$ ./a.out bulwark blue
bulwarke
$ ./a.out fresh feeling
freshling
$ ./a.out final01 test02
final01tes2
I tried a few ways, but can't figure how to do it without malloc. The tricky thing is, I cannot use malloc, the only authorized function is "write"
P.S : Sorry for my bad English
Use an auxiliary array to indicate which letter has already been used:
void func(char arr1[],char arr2[])
{
int hash[256] = {0};
for (int i=0; arr1[i]!=0; i++)
{
unsigned char letter = (unsigned char)arr1[i];
if (hash[letter] == 0)
{
hash[letter] = 1;
printf("%c",letter);
}
}
for (int i=0; arr2[i]!=0; i++)
{
unsigned char letter = (unsigned char)arr2[i];
if (hash[letter] == 0)
{
hash[letter] = 1;
printf("%c",letter);
}
}
printf("\n");
}
Note: this code assumes that each of the input strings (arr1 and arr2) is terminated with a 0 character.
Just keep track what you have printed. The follow will work just fine for char, because there are so few of them. Keeping track of unicode chars with an array that is long enough for all possible values would be mad. In such case inserting identifiers to a hash would probably be better option, but since unicode was not part of the consideration the following should be good enough.
#include <stdio.h>
int main(int argc, char **argv)
{
char arr[256] = { 0 };
int i, j;
for (i = 1; i < argc; i++)
for (j = 0; argv[i][j]; j++)
if (!arr[argv[i][j]]) {
arr[argv[i][j]] = 1;
printf("%c", argv[i][j]);
}
putchar('\n');
return 0;
}

Linear search arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How do I print multiple searches from a linear search in C?
for (i=0; i < index; i++)
if (array[i] == target)
return i;
Is it possible to return more then one value, say, if the array has multiple elements that equals the target?
You could go to the trouble of allocating a dynamic array within the function but that's probably something better left to the caller to manage.
I would change the function from something like:
int findVal (int *array, int size, int val) {
for (int i = 0; i < size; i++)
if (array[i] == val)
return i;
return -1;
}
to one that allowed you to specify a starting point (or, more precisely, one less than the starting point):
int findVal (int *array, int size, int last, int val) {
for (int i = last + 1; i < size; i++)
if (array[i] == val)
return i;
return -1;
}
then let your client call it with:
int index = findVal (myarray, sizeof(myarray)/sizeof(*myarray), -1, myval);
while (index != -1) {
// Do something with index.
index = findVal (myarray, sizeof(myarray)/sizeof(*myarray), index, myval);
}
If your client wants it in an array, they can put it in an array. But, if they just want to do something ephemeral (like just print the index then forget about it), it makes little sense to waste an array for that.
Instead of returning matching elements, you could print out their index values (allowing multiple values to be printed) or insert them into an array and then return that.

Resources