I'm trying to print out characters given input from the command-line arguments. I'm having a bit of trouble wrapping my head around this.
When I run ./test Foo bar I want it to print
./test
Foo
o
o
bar
a
r
While it might not be the best solution, I want it to be done using arrays of arrays of chars, and it looks like a three-dimensional array, so I'm going with a triple-nested for-loop.
What I have so far is this:
for (i = 1; i < argc; i++) {
for (j = 0; j < argv[argc][j]; j++) {
for (k = 0; k < argv[argc][j]; k++) {
printf("%c", k);
}
}
printf("\n");
}
The outermost loop starts at 1, since I don't want to print out the ./test-bit. But I'm lost. I can work with two-dimensional arrays, but I wanted to try it out with an extra dimension.
Can you give me a few pointers?
Your condition(j < argv[argc][j], k < argv[argc][j]) are wrong.
fix like this:
#include <stdio.h>
int main(int argc, char *argv[]){
for (int i = 0; i < argc; i++) {
puts(argv[i]);
if(i){
for(int j = 1; argv[i][j]; ++j){
printf("%c\n", argv[i][j]);
}
printf("\n");
}
}
return 0;
}
You can solve this in 2 loops as
for (i = 1; i < argc; i++) {
for (j = 0; argv[i][j]!='\0'; j++) {
printf("%c", argv[i][j]);
}
printf("\n");
}
Related
I am new to C programming....we have 2D arrays for integers...but how to declare and take the input for 2D string arrays...I tried it for taking single character input at a time similar to integer array...but I want to take whole string as input at a time into 2D array..
code:
#include <stdio.h>
int main()
{
char str[20][20];
int i, j;
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
scanf("%c", &str[i][j]);
}
}
}
can anyone resolve this problem?
The declaration of a 2D string array can be described as:
char string[m][n];
where m is the row size and n is the column size.
If you want to take m strings as input with one whole string at a time...it is as follows
#include<stdio.h>
int main()
{
char str[20][20];
int i,j;
for(i=0;i<m;i++)
{
gets(str[i]);
}
}
here 'i' is the index of the string....
Hope this answer helps...
A few issues with your code. Using scanf to reach characters, you're going to read newlines. If I create a more minimal version of your code with an extra few lines to print the input, we can see this:
#include <stdio.h>
int main() {
char str[3][3];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%c", &str[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%c ", str[i][j]);
}
printf("\n");
}
}
And running it:
$ ./a.out
gud
ghu
ert
g u d
g h
u
e
$
We can test the input to circumvent this. If the character input is a newline character ('\n') then we'll decrement j so effectively we've sent the loop back a step. We could easily extend this boolean condition to ignore other whitespace characters like ' ' or '\t'.
#include <stdio.h>
int main() {
char str[3][3];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
char temp = 0;
scanf("%c", &temp);
if (temp == '\n' || temp == ' ' || temp == '\t') {
j--;
}
else {
str[i][j] = temp;
}
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%c ", str[i][j]);
}
printf("\n");
}
}
Now,, when we run this:
$ ./a.out
gud
ghu
ert
g u d
g h u
e r t
$
Of course, one other thing you should do is to check the return value of scanf, which is an int representing the number of items read. In this case, if it returned 0, we'd know it hadn't read anything. In that case, within the inner loop, we'd probably also want to decrement j so the loop continues.
#include<stdio.h>
main()
{
char name[5][25];
int i;
//Input String
for(i=0;i<5;i++)
{
printf("Enter a string %d: ",i+1);
}
//Displaying strings
printf("String in Array:\n");
for(int i=0;i<5;i++)
puts(name[i]);
}
Simple code that accepts String in an array.
I'm trying to represent a 8x8 Cartesian plane whose contents would be a string of length 2. I'd like to keep as type-safety as I can this scheme and doing this:
typedef char cartesian[8][8][2];
cartesian xy;
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
xy[i][j][0] = ' '
xy[i][j][1] = ' '
}
}
// The first element would represent some kind of information, and the other one
// would be just elements like '+' or '*'. In other cases, this would be 'EMPTY',
// it means a double space.
xy[2][4][0] = 'B';
xy[2][4][1] = '+';
// The right printing method (cause it doesn't have any trash) would be:
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
printf("| %c%c ", xy[i][j][0], xy[I][j][1] );
}
printf("|\n");
}
But here's the question: Why the output trash with printf("| %s ", xy[i][j][]); ?
I know this could be a dumb question, but I'm out and tired now.
Thanks in advance.
By the way, doesn't work to assign. I mean: xy[2][4][] = "B+";.
Here is your code rewritten. Note that cartesian is now [8][8][3] and strcpy is used to fill the last array dimension. For clarity, I moved to clearing on xy into a separate function.
#include <stdio.h>
#include <string.h>
typedef char cartesian[8][8][3];
void clearXY(cartesian *xy)
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
strcpy((*xy)[i][j], " ");
}
}
}
int main(int argc, char *argv[])
{
cartesian xy;
// The first element would represent some kind of information, and the other one
// would be just elements like '+' or '*'. In other cases, this would be 'EMPTY',
// it means a double space.
clearXY(&xy);
strcpy(xy[2][4], "B+");
printf("-------- Method 1 --------\n");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("| %c%c ", xy[i][j][0], xy[i][j][1]);
}
printf("|\n");
}
printf("-------- Method 2 --------\n");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("| %s ", xy[i][j]);
}
printf("|\n");
}
printf("-------- Done ------------\n");
}
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);
how would I input a 2D array in C, filled with dots?
Here is the code I have written so far, but I still don't see an output of a 2D array with dots.
#include <stdio.h>
#include <stdlib.h>
#define PLAYER_NONE 0
#define PLAYER 1
#define PLAYER_CPU 2
int i; // Global Variable for column and row
int j; // Global Variable for column and row
char playerBoard[8][8]; //Global Variable
char cpuBoard[8][8]; // Global Variable
//Initialise the main parts of the board
void initialise_board(void)
{
for (int i = 0; i < 8; i++) { //iterate the rows
for (int j = 0; j < 8; j++) {
cpuBoard[i][j] = '.';
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
playerBoard[i][j] = '.';
}
}
}
int main(int argc, char *argv[])
{
initialise_board();
while (!check_win()) {
display_board();
get_move(turn);
turn = (turn == 1) ? 2 : 1;
return 0;
}
//Display battleship board when a turn is played
void display_board()
{
printf("\n");
for (char i = 'A'; i < 'H' + 1; i++) {
printf("%c", i);
}
printf("\n");
for (i = 1; i < 8 + 1; i++) {
printf("%d",i);
for (j = 0; j < 8; j++) {
}
printf("\n");
}
printf("===");
printf("\n");
}
Any help would be much appreciated, Thank you very much
Sometimes, while troubleshooting a seemingly impossible problem, it is a good idea to remove all distractions from the code:
Run a simplified main() function:
int main(int argc, char *argv[])
{
initialise_board();
//while (!check_win()) {
display_board();
// get_move(turn);
// turn = (turn == 1) ? 2 : 1;
return 0;
}
And you will see some output, which you can then debug to adjust as needed.
However, As you point out in your comments, you are not seeing output. If you look closely, you will discover that is because you are never including cpuBoard or playerBoard in a printf statement, such as:
printf("%c", cpuBoard[i][j]);
The following will not finish this for you, but will get you started:
void display_board(void)
{
//printf("\n");//removed for illustration
//for (char i = 'A'; i < 'H' + 1; i++) {
// printf("%c", i);
//}
printf("\n");
for (i = 1; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%c", cpuBoard[i][j]); //illustrates printout of populated array cpuboard.
}
printf("\n");
}
printf("===");
printf("\n");
}
Your code looks fine. Check for the opening and closing brackets and add following statement in initializeboard() function :
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c",playerBoard[i][j]);
}
}
Actually I did the easy part, but couldn't get more. I am a new Comp Eng student and we're trying to learn Loops. Our professor gave us this work to learn something. I did first part easily from last lesson. But I dont know how to lead it further.
I need to reflect this and get a full Bow Tie
Here it is my code:
#include <stdio.h>
main(); {
int sayi;
printf("Sayiyi gir > ");
scanf("%d",&sayi);
for (int i = 0; i < sayi; i++) {
for (int j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
for (int i = sayi; i >= 1; i--) {
for (int j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
return 0;
}
Change your code like:
for (i = 0; i <= sayi; i++) {
// Prints left part of the tie in row
for (j = 0; j < i; j++) {
printf("*\t");
}
// Prints the spaces between tie edges
for (j = 0; j < sayi - i; j++) {
printf("\t\t");
}
// Prints right part of the tie in row
for (j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
Repeat similar but reverse for lower part of the tie.