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.

Comments
thanks for posting a proper question, with formatted code
Attachments
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
/* 
   Author: Jaap Karan Singh for COMP1911
   Date created: 24/04/2010
   Real Estate
 */

#include <stdio.h>

void print_format(int option); // 1 = header, 0 = footer
void print_stage(int stage); //prints the stage number

int main(int argc, char *argv[]) {

  //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]);

  //simple for loop to print out all elements
  //int index = 0;
  //printf("|  %4d   %02d   %4d   %2d   %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);
  /*for (index=0; index < len; index++) {
    printf("| %4d %02d %4d %2d %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);    
  }*/

  print_format(0);

  /*//printing  sales that only happened in 2009
 
  print_stage(2);
  printf("Listing of all sales recorded for the year 2009:\n");
  print_format(1);
 
  index = 0;
  while (index < len) {
    if (home[index][2] == 2009) {
      printf("| %4d %2d %4d %2d %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);   
    }
    index++;
  }

  print_format(0); */

 /* //print number of sales for each postcode (minimum 1) with average price

  printf("---------------------------------------------------------------------------\n");
  printf("STAGE 3\n");
  printf("---------------------------------------------------------------------------\n");

  //introduce 2d array that record the number of sales and their average price in each postcode 
  int pcode_sale[][3]={}; // [0] = postcode, [1] = no of sales, [2] = total sales
  int pcode_len = 1, pcode_index;
  pcode_sale[0][0] = home[0][0]; //add first record manually so for loop initialises successfully
  
  for (index=0; index < len; index++) {  //it goes through the home array recording each element
    for (pcode_index=0; pcode_index < pcode_len; pcode_index++) {  //goes through pcode_sale array to check whether postcode already exists
      if (pcode_sale[pcode_index][0] == home[index][0]) {  
        pcode_sale[pcode_index][1]++;  //if it does exist increment number of sales
        pcode_sale[pcode_index][2] = pcode_sale[pcode_index][2] + home[index][4];  //and add to average 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];
      }    
    }
  }

  for (pcode_index=0; pcode_index < pcode_len; pcode_index++) {
    printf("Postcode %4d:    %d sale%cs%c took place with an average price of %8d\n", pcode_sale[pcode_index][0], pcode_sale[pcode_index][1], 40, 41, pcode_sale[pcode_index][2] / pcode_sale[pcode_index][1]);
  }*/

/*
  //sort all sales in ascending order according to price, year, month, postcode, bedrooms

  printf("---------------------------------------------------------------------------\n");
  printf("STAGE 4\n");
  printf("---------------------------------------------------------------------------\n");

  int ordered = 1; //0 means it is ordered
  while (ordered == 1) {
    ordered = 0;
    for (index = 0; index < len - 1; index++) {
      if (postcode[index] < postcode[index+1]) {
        int help = postcode[index];
        postcode[index] = postcode[index+1];
        postcode[index+1] = help;
        help = month[index];
        month[index] = month[index+1];
        month[index+1] = help;
        help = year[index];
        year[index] = year[index+1];
        year[index+1] = help;
        help = bedrooms[index];
        bedrooms[index] = bedrooms[index+1];
        bedrooms[index+1] = help;
        help = price[index];
        price[index] = price[index+1];
        price[index+1] = help;
        ordered = 1;
 //below else statement should be commented     
        } else if ((postcode[index] == postcode[index+1]) && (year[index] > year[index+1])) {
        int b = numArray[j][1];
        numArray[j][1] = numArray[j+1][1];
        numArray[j+1][1] = b;
        ordered = 1;
      }   
    }
  }
*/

/*  //determine median price depending on year

  printf("---------------------------------------------------------------------------\n");
  printf("STAGE 5");
  printf("---------------------------------------------------------------------------\n");
  
  int median[][2] = {0}, median_price_array[]={0};  //2d array that stores year, median price and no of sales
  int median_len, median_index, median_price_len;
  for (index = 0; index < len - 1; index++) {  //it goes through the year array recording each element
    for (median_index=0; median_index < median_len - 1; median_index++) {  //goes through median array to check whether postcode already exists
      if (median[median_index][0] == year[index]) {  
        median[median_index][1]++;  //if it does increment number of sales
      }
      else {  //else do the same except add new record
        median[median_len][0] = year[index];
        median[median_len][1] = 1;
      }    
    }
    median_price_array[median_price_len] = price[index];
    median_price_len++;
  }
//the above can be imporved by sorting the list before hand and then just using the normal index rather than median_price_index

//sort price array in correspondance with year
//fix the code and use the median_price array to store median price since right now it gets discarded. we can still print using below code in worst case but better to store the values in an array
  median_index = 0;  
  int median_price_index=0, median_price, sales = median[median_index][1];
  while (median_index < median_len - 1) {  //calculate median price and print it
    if ( (median[median_index][1] % 2) == 0) {  //if even number of sales, average of the two medians
      median_price = ( median_price_array[ (sales / 2) ] + median_price_array[ (sales / 2)+1 ] ) / 2; //the logic is written below
    } else {
      median_price = median_price_array[ (sales / 2) ]; //since array, it should be minus one but using integer type means another plus one. eg: if 5 sales, 3rd sale is median which is stored in array[2] for the first year.  However, when divind by 2 answer gives 2.5 where .5 is truncated hence making the answer 2 which is the memory location.
      median_price_index = sales - 1; //increment the median_price_array by the no of sales
    }
  }
  
  printf("Median sales price for %4d is %8d based on %d sales", 1,2,3);
*/

  //end of program
  return 0;
}

void print_format(int option) {
  if (option == 1) {
    printf("+=======================================+\n");
    printf("| Postcode Month Year Bedrooms    Price |\n");
    printf("+---------------------------------------+\n");
  } else {
    printf("+=======================================+\n");
  }
}

void print_stage(int stage) {
  printf("---------------------------------------------------------------------------\n");
  printf("STAGE %d\n", stage);
  printf("---------------------------------------------------------------------------\n");
}

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?

Comments
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.

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++) {

.

Edited 6 Years Ago by jephthah: n/a

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.

This article has been dead for over six months. Start a new discussion instead.