I'm actually trying to implement the floyd's warshall algorithm but I met a hard-point. I got a segmentation fault while i'm trying to find the shortest-way in my matrix of sequence. I follow this tutorial:
floyd's warshall algorithm
Everything work well except at the line 124:
chemin.insert(it,sequenceTest[v1-1][temp-1]);
where i get the segfault (You can skip the first part except if you want to know how I implement the algorithm butt the problem is in the second part):
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int i,j,k;
int sequenceTest [4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int tempSequenceTest [4][4];
int distanceTest [4][4] = {{0,2,4,100000},{2,0,1,5},{4,1,0,3},{100000,5,3,0}};
int tempDistanceTest[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<sequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << distanceTest[i][j] << " ";
}
cout<< endl;
}
cout<< endl;
for(k=0;k<3;k++)
{
cout <<endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << "i: " << i << " j: " << j << endl;
if(i == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
if(j == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
cout<< endl;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
//cout<< "i: " << i << " j: " << j << " k:" << k << endl;
if(i!=k && j!=k)
{
if(distanceTest[i][j]> distanceTest[i][k] + distanceTest[k][j])
{
tempDistanceTest[i][j] = distanceTest[i][k] + distanceTest[k][j];
cout << distanceTest[i][j] << " > " << distanceTest[i][k] << " + " << distanceTest[k][j] << " = " << tempDistanceTest[i][j] << " " << i << j << endl;
tempSequenceTest[i][j] = k+1;
}
else
{
tempDistanceTest[i][j] = distanceTest[i][j];
tempSequenceTest[i][j] = sequenceTest[i][j];
}
}
}
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
distanceTest[i][j] = tempDistanceTest[i][j];
sequenceTest[i][j] = tempSequenceTest[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<tempSequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << tempDistanceTest[i][j] << " ";
}
cout<< endl;
}
}
int v1, v2;
v1 = 1;
v2 = 4;
vector <int> chemin;
vector <int>::iterator it;
chemin.push_back(v1);
chemin.push_back(v2);
int temp = v2;
cout << sequenceTest[0][2];
while(sequenceTest[v1-1][temp-1] != v2)
{
it = chemin.begin() + 1;
cout << "V1: " << v1 << " V2: " << v2 << " Temp: " << temp << endl;
chemin.insert(it,sequenceTest[v1-1][temp-1]);
for(i=0;i<chemin.size();i++)
{
cout << chemin[i];
}
cout << endl;
temp = sequenceTest[v1-1][temp-1];
}
}
Thanks for your attention and for taking the time to help me!
chemin.insert(it,sequenceTest[v1-1][temp-1]);
This invalidates the iterator it, but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value.
it = chemin.insert(it,sequenceTest[v1-1][temp-1]);
Related
I'm doing Towers of Hanoi for school, which will eventually take 7 discs and I cannot figure out how to use cout statements to display the moves that the discs make. For example, "Move disk 7 from peg1 to peg3." Any help on this would be much appreciated. The cout statements would be in the moveDisk() and Hanoi() functions.
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
struct pegType
{
vector<int> diskStack;
string pegName;
};
/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg);
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg);
int main()
{
//Local Variables
pegType peg1, peg2, peg3;
peg1.pegName = "Peg1";
peg2.pegName = "Peg2";
peg3.pegName = "Peg3";
const int NUM_DISKS(3);
int totalMoves = 0;
//Welcome Message
cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
loadDisk(NUM_DISKS, peg1.diskStack);
//Check the conditions of the pegs in the beginning
cout << "The Starting Conditions of the three pegs: ";
cout << endl; printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
totalMoves = hanoi(NUM_DISKS, peg1.diskStack, peg3.diskStack, peg2.diskStack);
//Ending Conditions of the pegs and disks
cout << endl << "The Ending Conditions of the three pegs: " << endl;
printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
cout << endl;
system("pause");
return 0;
}
/***********************Function Definitions*****************************/
//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
for (int i = totalDisks; i > 0; i--)
{
startPeg.push_back(i);
}
}
//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
if (stack.size() > 1)
{
cout << name << " has " << stack.size() << " discs: ";
assert(stack.size() > 0);
for (unsigned int i = 0; i < stack.size(); i++)
{
cout << stack[i] << " ";
}
}
else
{
cout << name << " has " << stack.size() << " discs: ";
}
cout << endl;
}
//Moves the bottom disk to the goal
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg)
{
if (startPeg.size() > 0)
{
int temp = startPeg.back();
startPeg.pop_back();
goalPeg.push_back(temp);
}
}
//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg)
{
int count = 0;
if (totalDisks > 0)
{
count = hanoi(totalDisks - 1, startPeg, goalPeg, tempPeg);
moveDisk(startPeg, goalPeg);
count++;
count += hanoi(totalDisks - 1, tempPeg, goalPeg, startPeg);
}
return count;
}
Use struct Peg as parameters (instead of std::vector) in functions Hanoii() and MoveDisk() so that the function can correctly know the source and destination names for printing.
struct pegType
{
vector<int> diskStack;
string pegName;
};
/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(pegType& source, pegType& dest);
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux);
int main()
{
//Local Variables
pegType peg1, peg2, peg3;
peg1.pegName = "Peg1";
peg2.pegName = "Peg2";
peg3.pegName = "Peg3";
const int NUM_DISKS(3);
int totalMoves = 0;
//Welcome Message
cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
loadDisk(NUM_DISKS, peg1.diskStack);
//Check the conditions of the pegs in the beginning
cout << "The Starting Conditions of the three pegs: ";
cout << endl; printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
totalMoves = hanoi(NUM_DISKS, peg1, peg3, peg2);
//Ending Conditions of the pegs and disks
cout << endl << "The Ending Conditions of the three pegs: " << endl;
printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
cout << endl;
system("pause");
return 0;
}
/***********************Function Definitions*****************************/
//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
for (int i = totalDisks; i > 0; i--)
{
startPeg.push_back(i);
}
}
//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
if (stack.size() > 1)
{
cout << name << " has " << stack.size() << " discs: ";
assert(stack.size() > 0);
for (unsigned int i = 0; i < stack.size(); i++)
{
cout << stack[i] << " ";
}
}
else
{
cout << name << " has " << stack.size() << " discs: ";
}
cout << endl;
}
//Moves the bottom disk to the goal
void moveDisk(pegType& source, pegType& dest)
{
if (source.diskStack.size() > 0)
{
int temp = source.diskStack.back();
source.diskStack.pop_back();
dest.diskStack.push_back(temp);
std::cout << "Moving disk# " << temp << " from " << source.pegName << " to " << dest.pegName << '\n';
}
}
//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux)
{
int count = 0;
if (totalDisks > 0)
{
count = hanoi(totalDisks - 1, source, aux, dest);
moveDisk(source, dest);
count++;
count += hanoi(totalDisks - 1, aux, dest, source);
}
return count;
}
Output:
I have a function that returns a pointer to an array, and I want to print each value in the array
When I run it, it prints
int* merge_sort(int arr[], int size) {
if (size <= 1) {
return &arr[0];
}
int size1 = size/2;
int arr1[size1];
for (int i = 0; i < size1; i++) {
arr1[i] = arr[i];
}
int size2 = size-size1;
int arr2[size2];
for (int i = 0; i < (size2); i++) {
arr2[i] = arr[i+size1];
}
int p1 = 0;
int p2 = 0;
int sorted[size];
while (p1 < size1 && p2 < size2) {
if (arr1[p1] < arr2[p2]) {
sorted[p1+p2] = arr1[p1];
p1++;
} else {
sorted[p1+p2] = arr2[p2];
p2++;
}
}
while (p1 < size1) {
sorted[p1+p2] = arr1[p1];
p1++;
}
while (p2 < size2) {
sorted[p1+p2] = arr2[p2];
p2++;
}
cerr << "sorted: ";
for (int& i : sorted) {
cerr << i << ",";
}
cerr << endl;
return &sorted[0];
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int N;
int horses[N];
cin >> N; cin.ignore();
for (int i = 0; i < N; i++) {
int Pi;
cin >> Pi; cin.ignore();
horses[i] = Pi;
}
int *sorted = merge_sort(horses, N);
cerr << "~ " << sorted[0] << " " << sorted[1] << " " << sorted[2] << endl;
//cerr << "~~ " << sorted << " " << *sorted << " " << endl;
cerr << "^ " << *(sorted + 0) << endl;
cerr << "^ " << *(sorted + 1) << endl;
cerr << "^ " << *(sorted + 2) << endl;
for (int i = 0; i < N; i++) {
//cerr << "i " << i << endl;
// cerr << "SORTED?? " << sorted[i] << endl;
cerr << "value: " << *(sorted + i) << " ";
//cerr << "*** " << *(sorted) + i << endl;
}
cerr << endl;
}
When I run it, it prints
sorted: 5,8,9,
~ 5 8 9
^ -5728
^ 942815029
^ 6297320
value: 959592096 value: 0 value: -157570874
Why is my for loop not printing the values "5, 8, 9"? How can I fix it so that it does?
(Edited to be more detailed. Also, I realize my merge sort is wrong but I'm just trying to get it to return something I can use right now ^.^)
Make sure that you returned correct pointer, for example (if pointer should point to values):
std::cerr << "\n" << sorted << " "<< &values << "\n";
should return same values:
003DFD64 003DFD64
Seems ok. Check your sort function
cpp.sh/4rrk
// Example program
#include <iostream>
#include <algorithm>
const size_t N = 3;
int* sort(int arr[]) {
// Sort arr and it should be equal to [5, 7, 8]
std::sort(arr, arr+N);
return arr;
}
int main() {
int values[N];
values[0] = 7;
values[1] = 5;
values[2] = 8;
for (size_t i = 0; i < N; ++i) {
std::cout << *(values + i) << " ";
}
std::cout << std::endl << values[0] << " " << values[1] << " " << values[2] << std::endl;
int *sorted = sort(values);
std::cout << "It is same arrays: " << values << " " << sorted << std::endl;
for (size_t i = 0; i < N; ++i) {
std::cout << *(sorted + i) << " ";
}
std::cout << std::endl << sorted[0] << " " << sorted[1] << " " << sorted[2] << std::endl;
return 0;
}
Output:
7 5 8
7 5 8
It is same arrays: 0x7329bfdc1d90 0x7329bfdc1d90
5 7 8
5 7 8
Very new to programming.
This bit of my program accepts two strand of DNA as input and output them in a double helix drawing. The problem is, if one of the two input strand is longer than the other, i will receive error.
So I thought, is it possible that if strand[add] is non-existent anymore, replace it with *?
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void helix(string &strand1, string &strand2)
{
int nucleo;
int length;
if (strand1.length() >= strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
int add;
for (int add = 0; add <= length - 1; add++)
{
if (add > 7)
{
nucleo = add % 8;
}
else
{
nucleo = add;
}
if (nucleo == 0)
{
cout << " " << strand1[add] << "---"<<strand2[add] << endl;
}
else if (nucleo == 1)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 2)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 3)
{
cout << " " << strand1[add] << "---" << strand2[add] << endl;
cout << " *" << endl;
}
else if (nucleo == 4)
{
cout << " " << strand2[add]<<"---" << strand1[add] << endl;
}
else if (nucleo == 5)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 6)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 7)
{
cout << " " << strand2[add]<<"-----" << strand1[add] << endl;
cout << " *" << endl;
}
}
}
int main()
{
string strand1,strand2;
cout << "ENTER STRAND:" << endl;
cin >> strand1;
cout << "ENTER STRAND:" << endl;
cin >> strand2;
helix(strand1,strand2);
_getch();
return 0;
}
I was hoping I could still show the longer strand even if the other side of the strand is empty(want to put *) like this :imgur.com/t7riVrS
I think you inverted the legnth test, it should be:
//if (strand1.length() >= strand2.length())
if (strand1.length() < strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
Edit:
If you want it fill one the string with '*', replace the code above with:
while (strand1.length() < strand2.length())
{
strand1 += "*";
}
while (strand1.length() > strand2.length())
{
strand2 += "*";
}
I'm working on an assignment for school. The code is supposed to read form a file and create an array, then sort the values of the array to output certain info. It works just fine as long as I have 3+ lines of info in the file. If not, I get the following error:
First-chance exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
Unhandled exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
I can't figure out why, any help would be appreciated. Here's the code:
#include <iostream> //calls the information needed
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std; //sets all unmarked commands to std::
const int ARRSIZE = 1000;
struct Student
{
string firstName;
string lastName;
string id, temp;
double gpa;
};
int readArray(ifstream& ifile, Student arr[]);
void swapElements(Student arr[], int i, int j);
void sortArray(Student arr[], int numberInTheArray);
int main()
{ // Declares the needed variables
double sought, min, max;
int i, ival, returnvar, count = 0, mincount, maxcount;
string filename;
ifstream ifile;
Student arr[ARRSIZE];
cout << "Input File Name: ";//requesting the file name
cin >> filename;
ifile.open(filename.c_str());//opening the file
if (!ifile)//checking if it opened or not
{
cout << endl << "That file does not exist!" << endl;//informing the user it did
return 1;//not open and returning 1
}
cout << "Which number do you want to return? ";//requesting the desired number
cin >> ival;
i = ival - 1;
cout << endl;
returnvar = readArray(ifile, arr);
min = arr[0].gpa;
max = arr[0].gpa;
sought = arr[0].gpa;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
if (count == 0)
{
cout << "The file is empty!" << endl;
return 1;
}
cout << "Before Sort:" << endl;
cout << " Min GPA is " << min << " for " << arr[mincount].lastName << "." << endl;
cout << " Max GPA is " << max << " for " << arr[maxcount].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortArray(arr, returnvar);
count = 0;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
cout << "After Sort:" << endl;
cout << " Array[0] GPA is " << min << " for " << arr[0].lastName << "." << endl;
cout << " Array[" << (returnvar - 1) << "] GPA is " << max << " for " << arr[(returnvar - 1)].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
return 0;
}
int readArray(ifstream& ifile, Student arr[])
{
int counter = 0;
while ((ifile) && (counter <= ARRSIZE))
{
ifile >> arr[counter].firstName;
ifile >> arr[counter].lastName;
ifile >> arr[counter].id;
ifile >> arr[counter].gpa;
counter++;
}
return (counter - 1);
}
void sortArray(Student arr[], int numberInTheArray)
{
for (int i = 0 ; i < numberInTheArray - 1; i++)
{
for (int j = 0 ; j < numberInTheArray - 1; j++)
{
if ( arr[j].gpa > arr[j + 1].gpa)
{
swapElements(arr, j, j+1);
}
}
}
}
void swapElements(Student arr[], int i, int j)
{
Student temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Please ignore the insanity and comments. Like I said, for an entry level course.
Try replacing counter <= ARRSIZE with counter < ARRSIZE (a rule of thumb in C is: never use <= in operations related to container sizes).
EDIT: also in your main(), you must check that i < ARRSIZE (equivalently, return error if i >= ARRSIZE). At present you seem to accept the case i == ARRSIZE, which is also wrong. And finally, readArray should return counter (that is, one more than the last written index).
This is supposed to read at most 1000 numbers from a file into an array and then analyze them. It works flawlessly unless I use a file with a million numbers in it. I probably because I have an infinite loop, but I can't find where. I'm not supposed to go over 1000 elements.
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
const long N=1000;
using namespace std;
//declaring the functions
int readArray(ifstream& ifile, long arr[]);
void sortArray(long arr[], int numberInTheArray);
int main()
{
//variable declaration
int n=0;
int i=0;
int numberInTheArray=0;
long minimum=0;
long maximum=0;
long arr[N]={0};
ifstream ifile;
string strVar;
cout << "Input File Name: ";
cin >> strVar;
cout << "Which number do you want to return? ";
cin >> i;
cout << endl;
ifile.open(strVar.c_str());
if(!ifile)
{
cout << "That file does not exist!" << endl;
return 1;
}
numberInTheArray = readArray(ifile,arr);
if (numberInTheArray == 0){
cout << "The file is empty!" << endl;
}
else{
maximum = arr[n];
minimum = arr[n];
n++;
while (n<=N){
if (arr[n] <= minimum){
minimum = arr[n];
}
if (arr [n] >= maximum){
maximum = arr[n];
}
n++;
}
cout << "Before Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
sortArray(arr,numberInTheArray);
cout << "\nAfter Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
}
return 0;
}
int readArray(ifstream& ifile, long arr[])
{
int n=0;
long num;
while (n<=N && ifile){
ifile >> arr[n];
n++;
}
n=n-1;
return n;
}
void sortArray(long arr[], int numberInTheArray)
{
int a;
int b;
int temp;
for (a=0; a<numberInTheArray; a++)
{
for (b=0; b<numberInTheArray; b++)
{
if (arr[a] < arr[b])
{
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}
}
The problem is your loop conditions n <= N. Remember that array indexes goes from zero to size minus one, so the condition should be n < N.