Hey,

I am new here. I just started coding in C a couple of weeks ago. I am trying to create a simple program to input data into an array using a text file with 10 records. However, the array length is 16 and a couple of records in the middle have random generated data. Please help. Thanks.

Sample text file:
Postcode Month Year Bedrooms Price
2081 12 2007 4 795500
2180 7 2009 6 165000
2140 12 2008 5 689000
2153 3 2008 4 491500
2140 2 2010 5 886500
2005 11 2007 4 776500
2151 7 2007 5 305500
2199 12 2010 5 895000
2101 4 2010 7 389500
2176 2 2008 2 959500

//skip the header line
  while (getchar()!='\n') {
  }

  int len = 0; //total number of records inputted
  int home[][5]={{0},{0},{0},{0},{0}}; //introduce the array, [0] = postcode, [1] = month, [2] = year, [3] = bedrooms, [4] = price
  int input = scanf("%d%d%d%d%d", &home[len][0], &home[len][1], &home[len][2], &home[len][3], &home[len][4]);
  
  while (input != EOF) { 
    len++;
    input = scanf("%d%d%d%d%d", &home[len][0], &home[len][1], &home[len][2], &home[len][3], &home[len][4]);
  }

  //printing inputted information into table form
  print_format(1);
  printf("%d", len);
  printf("|  %4d   %02d   %4d   %2d   %8d |\n", home[7][0], home[7][1], home[7][2], home[7][3], home[7][4]);

P.S. Also, I get an error when printing one record of an array. eg i can print fine using home[0][0] but have a problem when i use a = 0; home[a][0]; it gives a segmentation error.

jephthah commented: thanks for posting a proper question, with formatted code +7

declare your variables at the top of main()... before your while loop.

declare your array 'home' size properly.

Thanks a lot for that. Simply putting the number of rows instead of leaving it empty did the trick. I have one more question:

I am trying to create one more array which looks at this home array and records this into a new array based on postcode. If it already exists, it adds to the number of sales. However, again this segmentation fault keeps stuffing me over.

I 'd appreciate any help.

//introduce 2d array that record the number of sales and their average price in each postcode 
  int pcode_sale[10000][3]={}; // [0] = postcode, [1] = no of sales, [2] = total sales
  int pcode_len = 0; int pcode_index = 0;  int exist = 0;

  pcode_sale[0][0] = home[0][0];
  pcode_sale[0][1] = 1;
  pcode_sale[0][2] = home[0][4];
  
  for (index=1; index < len; index++) {  //it goes through the home array recording each element
    while (pcode_index < pcode_len || exist == 0) {  //goes through pcode_sale array to check whether postcode already exists
      if (pcode_sale[pcode_index][0] == home[index][0]) {  
        exist = 1;
      }
      pcode_index++;
    }
    if (exist == 1) {
      pcode_sale[pcode_len][1] = pcode_sale[pcode_len][1] + 1;  //if it does exist increment number of sales
      pcode_sale[pcode_len][2] = pcode_sale[pcode_len][2] + home[index][4];  //and add to total price
    } else {  //else add new record
      pcode_len++;
      pcode_sale[pcode_len][0] = home[index][0];
      pcode_sale[pcode_len][1] = 1;
      pcode_sale[pcode_len][2] = home[index][4];
    }    
  }

Did you forget to reset pcode_index and exist?

commented: my bad, it's a stock icon. i "edited it" for my LinkedIn profile. anyhow, thanks for changing your pic. its sad that a cartoon icon is my identity. i need to reassess my life priorities. +7

mate i see what u mean, after my own post I realised about the index and after I looked at yours I have neutralised exist as well but to no avail.

I have tried debugging it so many different ways to no avail. Think I might have to give up and look at someone else's way of doing the same thing.

If it makes it easier, my logic is to go through every single record in the home array and add it to postcode array. If the postcode exists, it simply adds to the number of sales and then movews onto the next record.

My code right now:

//introduce 2d array that record the number of sales and their average price in each postcode 
  int pcode_sale[10000][3]={}; // [0] = postcode, [1] = no of sales, [2] = total sales
  int pcode_len = 0;
  int pcode_index = 0;  
  int exists = 0;

  pcode_sale[0][0] = home[0][0];
  pcode_sale[0][1] = 1;
  pcode_sale[0][2] = home[0][4];
    
  for (index=1; index < len; index++) {
    while (pcode_index < pcode_len && exists == 0) {  //goes through pcode_sale array to check whether postcode already exists
      if (pcode_sale[pcode_index][0] == home[index][0]) {  
        exists = 1;
      } 
      pcode_index++;
    }
    //printf("%d", pcode_index);
    if (exists == 1) {
      printf("yeh");
      //pcode_sale[pcode_len][1] = pcode_sale[pcode_len][1] + 1;  //if it does exist increment number of sales
      //pcode_sale[pcode_len][2] = pcode_sale[pcode_len][2] + home[index][4];  //and add to total price
    } else {  //else add new record
      pcode_len++;
      pcode_sale[pcode_len][0] = home[index][0];
      pcode_sale[pcode_len][1] = 1;
      pcode_sale[pcode_len][2] = home[index][4];
    }
    exists = 0;
    pcode_index = 0; //neutralise pcode for next record  
  }

the problem here is that an array index is increasing beyond the boundaries of your array size, and trying to access memory that has not been allocated.

fix this

for (index=1; index < len; index++) {

.

and chiwawa10, i'm flattered that you feel the urge to copy me, but please go ahead and find yourself another icon/avatar to use, and not steal mine.[/B].

that's not a stock avatar; it's not a coincidence that you picked the same avatar from some pool of images. that's one that i searched out on my own and personally trimmed to fit the specific pixel ratio for this site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.