Error with newarray

closed account (GybDjE8b)
Basically i need to sort the birthday, so i added them all up to get one array to be able to use quick sort..... but it says my newarray has an error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <stdlib.h> 
#include <iomanip>

using namespace std;
struct date
{
    int month,year,day,total;
};

void print(int a[],int size) {
  for (int i = 0; i < size; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

int partition(int a[], int p, int r) {
  int x = a[r];
  int j = p - 1;
  for (int i = p; i < r; i++) {

    if (x >= a[i]) {
      j = j + 1;
      int temp = a[j];
      a[j] = a[i];
      a[i] = temp;
    }
  }
  a[r] = a[j + 1];
  a[j + 1] = x;

  return (j + 1);
}

void quicksort(int a[], int p, int r) {
  if (p < r) {
    int q = partition(a, p, r);
    quicksort(a, p , q - 1);
    quicksort(a, q + 1 , r);
  }
}


int main()
{
    const int n = 10;
    int id[n];
    fstream bday;
    date array[n];
    bday.open("Input.txt");
    
	cout << "ID" << " : " << setw(2) << "Month" << " , " << setw(2) << "Day" << " , " << setw(2) << "Year" << endl;

    for(int i = 0; i < n; i++)
    {
		id[i] = i+1;
        array[i].month = rand()%12 + 1;
        array[i].year = rand()%501 + 1500;
        array[i].day = rand()%30 + 1;
        bday << id[i] << array[i].month << array[i].day << array[i].year;
        cout << id[i] << setw(2) << " : " << setw(3) << array[i].month<< setw(6) << " , " << setw(3) 
             << array[i].day << " , " << setw(4) <<array[i].year << endl;
    }
    
    
    bday.close();
    int newarray[n];
    for(int j = 0; j < n; j++)
	{
    array[j].total = (360*array[j].year) + ((array[j].month-1)*30) + array[j].day;
    cout << array[j].total << " ";
    newarray[j] = array[j].total;
    }
    cout << endl;

    quicksort(newarray, 0,n);
    print(newarray, n);

   
  system("PAUSE");
  return 0;
}

Last edited on
Could you provide us with the error? Copy paste it.
closed account (GybDjE8b)
it gives this as an out put the array 1 : -471057316 2 : 550212 3 : 558038 4 : 560830 5 : 580916 6 : 586747 7 : 642116 8 : 662290 9 : 686398 10 : 691644

1: is wrong

Run-Time Check Failure #2 - Stack around the variable 'newarray' was corrupted
Last edited on
Topic archived. No new replies allowed.