Home > Software design >  can I somehow store a variable amount of characters in an string array?
can I somehow store a variable amount of characters in an string array?

Time:11-14

#include <stdio.h>
#include <stdlib.h>

#define INITIAL_SIZE 3



int main()
{
    size_t arraySize = INITIAL_SIZE;
    size_t numsEnteredSoFar = 0;
    int* myArray = malloc(sizeof(*myArray)*arraySize);  
    if (myArray == NULL) exit(-1);  

    while(1)
    {
        int curEntry;
        char ch=0;
        if (scanf("%d", &curEntry) == 1)
    
    
        {
            ch = fgetc (stdin);
            
            myArray[numsEnteredSoFar  ] = curEntry;
        

            if (numsEnteredSoFar == arraySize)
            {
            
            arraySize  = INITIAL_SIZE;
            int* temp = realloc(myArray, arraySize*sizeof(*myArray));
            if (temp == NULL)
            {
                fprintf(stderr, "out of memory\n");
                exit(-1);
            }
            else
            {
            
                myArray = temp;
            }
        }
    }
     
    if(ch == 10)
    {
        
        break;
    }
    if(myArray[numsEnteredSoFar]>10000 || myArray[numsEnteredSoFar]<-10000)
{
   
   exit (-1);
}
    }



 int i,  found=0, pos=0,max=0, min=0,nm=0,mez=0;

  float pk, pz, ps, pl, prumer;
  char space=32;
  for (size_t i = 0; i < numsEnteredSoFar; i  ) 
  {
  
      nm  ;
    if(i >3)
  {
    exit (-1);  
  }
  }
    if(myArray[i]>=3 && myArray[i]<=69 )
    {
        if(nm==1)
        {
        printf("XXXXX\n");
        printf("X   X\n");
        printf("X   X\n");
        printf("X   X\n");
        printf("XXXXX\n");
        }
        if(nm==2)
        { 
        
            for(i=0; i<myArray[1]; i  )
            {
              mez  ;
            printf("X");
            }
                 printf("\n");
          
        for (i=0; i<myArray[0]-2; i  )
        {
         printf("X");
         for(i=0;i<mez-2;i  )
         {
         printf(" ");
         }
         }
        printf("X");
         printf("\n");  
     
     
            for(i=0; i<myArray[1]; i  )
            {
            printf("X");   
            }
             printf("\n");
         }
        }
        



    printf("%d\n", nm);
     printf("%d\n", mez-2);
    printf("%d\n", myArray[0]);

    for (i = 0; i < numsEnteredSoFar; i  )
     {
       if (i)
       printf (", ");
       printf ("%i", myArray[i]);
     }
 
  printf ("\n");  









 free(myArray);
}

first number is height second is width

any clue on what am I missing? its supposed to output this

enter image description here

but it outputs this

enter image description here]3

I need the line 2 copied and printed below so it would make a rectangle

my idea was this

printf("X%sX", ch)//where "ch" would be variable number of spaces 

continuation:how do you make a isosceles triangle using for loops?

CodePudding user response:

Take the rows one-by-one. You have to print the full line of 'X's on the first and last row. For all middle rows, you want to print 'X' as the first and last character, and then spaces in between.

You can lay your loops out as follows (there is more than one way to structures the test conditions), e.g.

#include <stdio.h>

#define NELEM 5     /* if you need a constant, #define one (or more) */

int main (void) {
  
  for (int i = 0; i < NELEM; i  ) {       /* for NELEM rows */
    for (int j = 0; j < NELEM; j  ) {     /* for NELEM columns */
      if (i == 0 || i == NELEM - 1)       /* 1st or last row */
        putchar ('X');
      else {                              /* middle-rows */
        if (j == 0 || j == NELEM - 1)     /* 1st and last char */
          putchar ('X');
        else
          putchar (' ');                  /* rest */
      }
    }
    putchar ('\n');
  }
}

Example Use/Output

$ ./bin/xbox
XXXXX
X   X
X   X
X   X
XXXXX

Let me know if this is what you are after. If not, I can help further.

A shorter, but less readable version using a ternary could be written as:

#include <stdio.h>

#define NELEM 5     /* if you need a constant, #define one (or more) */

int main (void) {
  
  for (int i = 0; i < NELEM; i  ) {       /* for NELEM rows */
    for (int j = 0; j < NELEM; j  ) {     /* for NELEM columns */
      /* 1st or last row/col 'X' otherwise ' ' */
      putchar ((i == 0 || i == NELEM - 1 || j == 0 || j == NELEM - 1) ?
              'X' : ' ');
    }
    putchar ('\n');
  }
}

(same output)

There are many ways to skin-the-cat.

  •  Tags:  
  • c
  • Related