mardi 28 novembre 2017

C Coding: Create Char Array, Print, Reorganize, and Print Again

I would like to create a char array, print it, reorganize it, and then reprint it in C. Here's what I have so far:

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

int main(){

  int i = 0;
  int j = 0;
  char k[4][1];
  char thing[1][1];

  strcpy(k[0] , "A");
  strcpy(k[1] , "B");
  strcpy(k[2] , "C");
  strcpy(k[3] , "D");

  printf("\nThe ordered structure is: \n");
  for (int i = 0; i < 4; i++) {     // fill structure
      printf("%s,", k[i]);
  }

  printf("\nThe intial structure is: \n");
  for (int i = 0; i < 4; i++) {    // reorder structure
    strcpy(thing[1], k[i]);
    j = (int)(i + rand() / (RAND_MAX / (5 - i)) );

    strcpy(k[i], k[j]);
    strcpy(k[j], thing[1]);
    printf("%s,", k[i]);  // print structure
  }

  return(0);

}

But I get a bunch of errors and warnings.




Aucun commentaire:

Enregistrer un commentaire