C :: Insertion Sort Function Crashing If 8 Or More Values?

Jan 15, 2015

This program I'm working on accepts an array size from the user, prompts the user to store that many integers, sorts them from smallest to largest, and then searches for duplicates with a simple for loop. The ultimate goal of the assignment was to display duplicates in an array, and the rest of the functions are just how I decided to reach that goal.

Anyway, my program crashes if I choose an array size larger than 7. It sorts and displays duplicates perfectly with 7 or fewer arguments.

The exact moment it crashes is after I enter the final value it prompts me for, so it appears my inputsize() function and my inputarray() function are working, and the error may be in the arrsort() function. Code is below:

Code:
#include <stdio.h>
int funcinputsize(int);
void funcinputarray(int [], int size);
void funcarrsort(int [], int size);
void funcdupe(int [], int size);

[Code] ...

View 4 Replies


ADVERTISEMENT

C :: Concept Of Insertion Sort - Verification

Apr 7, 2014

Can validate if my code implements the concept of insertion sort. The program is executing successfully. Just need a verification!

Code:

int i,j,k,a[100],n,num,min,max,temp;
printf("Enter array size: ");
scanf(" %d",&n);
printf("
Enter the numbers:");
for(i=0;i<n;i++)
scanf(" %d", &a[i]);

[Code] ....

View 9 Replies View Related

C :: Using Insertion Sort For Sorting Database By Age

Apr 29, 2014

I am trying to sort a student database by age,but i am not sure whats wrong i think it has to deal with tmpstudent variable.

Code:
void insertion_sort(StudentDB *db) {
int i;
for (i = 0; i < db->num; ++i) {
int j = i - 1;
int val = db->records[i].age;

[Code] .....

View 1 Replies View Related

C/C++ :: Two Dimensional Array Using Insertion Sort?

Apr 3, 2014

I want to use two dimensional array by insertion sort or quick sort

View 2 Replies View Related

C :: Insertion Sort In Ascending Order Not Working

Jun 20, 2014

I am having trouble sorting out a list of names in c. I have code for sorting the names, but when I go to print them out they still are in the same order as they were at the beginning so something isnt right. So the function that I need is the sort_data function.

Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LEN 25
void insert_data(char **strings, const char* filename, int size);
void allocate(char ***strings, int size);

[Code] ....

The list that I am reading in is as follows:

matt
susan
mark
david
aden
phil
erik
john
caden
mycah

So I need to get this list in alphabetical order, but when I run my code and print out this list after I run the sort function, they are still in this order.

View 5 Replies View Related

C++ :: Insertion Sort Program - Runtime Error

Apr 11, 2013

I am trying to run this program for Insertion Sort, But some how I am getting some problem:

#include<iostream>
int main(){
int i,j,key,n;
scanf("%d",&n);
int a[n];

[Code] .....

Error
In function 'int main()':
Line 10: error: ISO C++ forbids variable-size array 'a'
compilation terminated due to -Wfatal-errors.

View 2 Replies View Related

C++ :: Insertion Sort Algorithm With Comparison Counter

Nov 7, 2013

So I have an insertion sort function implemented that sorts through an array, but I'm having a problem showing the correct number of comparisons to work.

Each time I'm checking a value with another, the counter should update.

For instance, having an array of 10 elements going from 10-1 should give 45 comparisons, but I'm getting 54 comparisons.

void insertionSort(int a[], int& comparisons, const int& numOfElements) {
int j, value;
for (int i = 1; i < numOfElements; i++) {
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)

[Code] .....

View 3 Replies View Related

C++ :: Sorting With Selection / Insertion And Bubble Sort

May 4, 2014

This program using the selection, insertion, and bubble sorts. The program needs to be able to do the following:

1. Create an array of 1000 population records when the array object is instantiated. Call it unSorted.

2.Open the file called "Population.csv" (on the portal) and invoke a function that loads the population data into the array.

3.Create a second array of 1000 elements that will be used to sort the data using the different algorithms. Name is sortedArray.

4.Write a function that will copy unSorted into sortedArray and execute that function.

5.Using a function, display the unsorted array.

6.Invoke the insertionSort () function that will sort the sortedArray using the insertion sort algorithm. Sort the population data on the rank field in ascending order. Alternatively, you can sort in descending order on population.

7.Using the display function, display sortedArray.

8.Display the number of iterations it took to do the sort using this algorithm.

9.Repeat steps 4-8 for the selection and bubble sort algorithms.

Here is my code so far:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void loadArray (int unSorted[], int s);
void displayArray (const int num [], int size);

[Code] .....

Here is a few lines from the Population.csv file contents:

Code:
Alabama,Baldwin County,140415,389
Alabama,Blount County,51024,908
Alabama,Calhoun County,112249,477
Alabama,Colbert County,54984,858
Alabama,Cullman County,77483,653
Alabama,Dale County,49129,927
Alabama,Dallas County,46365,974
Alabama,DeKalb County,64452,753

I'm not sure how to load the data from the file into the array properly, I attempted this. I also don't know how to copy the unSorted into sortedArray.

View 14 Replies View Related

C++ :: Heap Sort Algorithm Crashing After 4100 Entries?

Nov 18, 2013

I developed the following heap sort algorithm code, and for some reason anytime it goes above 4100 entries, the algorithm completely crashes. It works perfectly up until that point but I can't see why it would crash?

void heap_from_root(MVector &v, int i, int n) {
int end=n,j=0;
// Identify the lowest root and how many sons it has. If it only has one son, set j=1.
if (n==1) { n = 0; j = 1; }
else if ((n-2) % 2 == 0) { n = (n-2)/2; }
else if ((n-1) % 2 == 0) { n = (n-1)/2; j=1; }

[Code]...

View 6 Replies View Related

C++ :: Sorting Array Of Numbers Into Ascending Order - Bubble Sort Keeps Crashing

Oct 29, 2014

I am trying to write a program which will sort an array of numbers into ascending order, here is my code

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int array[]={1, 5, 17, 3, 75, 4, 4, 23, 5, 12, 34, 34, 805, 345, 435, 234, 6, 47, 4, 9, 0, 56, 32, 78};

[Code] .....

This compiles fine but when I run the .exe for the first time an error message comes up saying program has stopped working. If I run the program again without recompiling it seems to work as expected.

View 2 Replies View Related

C :: Create Function That Allows Insertion Anywhere In Linked List

Feb 18, 2013

I'm trying to create a function that allows insertion anywhere in a linked list...here is what I have so far but am a bit confused on where to go from here

Code:
void llAddAtIndex(LinkedList** ll, char* value, int index) {
LinkedList* newNode = (LinkedList*)malloc(sizeof(ll));
newNode = *ll;
LinkedList* prevNode = (LinkedList*)malloc(sizeof(ll));

[Code].....

View 7 Replies View Related

C++ :: Console App Crashing After Calling Function

Jul 18, 2014

Im new to debugging but it says the problem is on line 97 which is the verb menu function being called in the first switch statement case 0:

verb menu is one of the sub menus i want ,and it loads but then it crashes ...

#include <iostream>
#include <string>
#include "spanishverbs.h"
#include <windows.h>
#include <cmath>
#include <cstdlib>
using namespace std;
void nounmenu();

[Code] ....

View 3 Replies View Related

C :: Passing Array To Function And For Loop Crashing

Sep 23, 2014

The first problem i'm having is in the following code.

Code: #include<stdio.h>
#include<stdlib.h>
#include<time.h>
// program ask for max value and always returns random numbers < max

[Code].....

The ultimate goal of these two programs is to merge them and make one program that takes a max value from the user and fills up an array of (10^6) elements with values ranging from (0 to maxValue).

View 7 Replies View Related

C/C++ :: Program Crashing On A Function Call In Main?

May 5, 2014

i have this program I am working on and it seems to crash after the function call getdata()

here is the code

#include<iostream>
#include<string>
#include<cstdlib>

[Code].....

View 1 Replies View Related

C :: Radix Sort Function To Sort Array Of 64 Bit Unsigned Integers

Aug 19, 2013

Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.

Code:
typedef unsigned long long UI64;
typedef unsigned long long *PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) {
size_t mIndex[8][256] = {0};
/* index matrix */
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;

[Code]....

View 9 Replies View Related

C++ :: Write Prototype Of A Member Function To Overload Insertion Operator

Apr 10, 2014

Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).

#include <string>
using namespace std;
class StudentTestScores{
private:
string studentName;
float *testScores; // used to point to an array of test scores
int numTestScores; // number of test scores

[code]....

View 1 Replies View Related

C++ :: How To Sort A 1D Array Using Function Sort

Mar 22, 2013

how to sort a 1D array using function sort().but if i have a 2D array how do i sort only 1 row of it (or column)?

View 2 Replies View Related

C :: Sort The Array Values Then Throw Out First And Last Value

May 1, 2014

This program is suppose to sort the array values then throw out the first and last value. It does not seem to do doing this.

Code:
# include <stdio.h>02
void bubbleSort(float judges[], float array);
03
int main (void){

[code] .....

View 3 Replies View Related

C/C++ :: Reading Binary Files Using Fstreams - Sort Values

Dec 11, 2014

I have written in a binary file using fstream an using sort value.

unsigned shortwidth=50,height=50;
file.write((char*)&width,sizeof(width));
file.write((char*)&height,sizeof(height));

When i try to read it back from fstream again there are some symbols (binary obviously). How can i get my values back? I want to read those symbols and in a way to convert them to my old width and height values.

View 6 Replies View Related

C :: Simple Program To Bubble Sort Values Of A Linked List

Nov 30, 2013

HelI have been tasked with creating a program which (1) takes in integer values from a user (until the user enters -1) and inputs these values into a linked list. This (2)original list is then to be printed out. The program then uses the algorithm "bubble sort" to (3)order the list in descending order before finally printing it out again.

I have managed to do this but I kind of cheated since I do not quite understand how to manipulate a linked list. What did was I took the values in the linked list and transferred them into an array and then did bubble sort on that array.how to do bubble sort on a linked list as well as how to print a linked list.

Code:

#include<stdio.h>#include<stdlib.h>
typedef struct node
{
int info;
struct node *link;

}Node, *NodePointer;
void printList (NodePointer head)
}

[code]...

View 4 Replies View Related

C++ :: Syntax Error In Function Call - Type Mismatch In Parameter In Function Sort

Jul 6, 2014

error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)

Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();

[Code] .....

View 11 Replies View Related

C :: How To Make A Sort Function

Feb 28, 2013

Im suppose to make a "poor mans" variation to the Sort function built into unix. The program is suppose to read in a file and sort the contents of the file. So its a variation of the Unix Sort feature. I have remade the readLine function we were provided so that it doesnt use fgets. where to go from here, Not sure on how to make a sort function. Here are the reqirements of the program:

Code:

• Re-implement the readLine() function so that it no longer makes use of fgets(). Instead,
process the input on a given line character-by-character.

• Provide code which will create a data structure similar to argv to hold all of the words to be sorted. Use malloc() to ensure that each entry has just the required number of bytes needed to store the words. The final entry in your array should be the NULL pointer.

• Implement a sort() function which will rearrange the words in sorted order. To swap two words in your array, note that only a pair of pointers need to move. The strings themselves, once established, will never move. Heres what i have:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINES 1000 /* maximum number of reminders */
#define WORD_LENGTH 10 /* max length of reminder message */

[code]....

View 9 Replies View Related

C :: Multiplication With Sort Function

Oct 30, 2014

I need to include validation to only accept numbers and to only the last four odd numbers in the table.

Code:

#include <stdio.h>void main() {
int upper, range, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&upper);

[Code].....

View 3 Replies View Related

C++ :: Standalone Sort Function For The Class

Apr 30, 2013

I need to write a standalone sort function, which will take a linked list parameter and sorts it. The function is included in the main list class as friend class: Code: friend void sort (UList<T>&) I need to submit separate SortIS.h file for insertion sort. Here is my code:

Code:

#include <iostream>
template <class T>
void SortIS(UList<T>& list)
{
T temp;
for (int i=1; i<list.size(); i++)
{
temp = list[i];

[Code]...

But the teacher said it didn't compile returning an error: "undefined reference to `void". What I am missing here?

View 6 Replies View Related

C :: Sort Array By Function To Get Max Number

Mar 6, 2015

I have this code(homework) i've been working on for several days i couldn't fix this error.

Question is: to write a sort program in c . Ineed to ask user for how many numbers to be sorted(n) ask again for values. The approach of sort is this:

1- store n in temporary var (temp)
2- search for largest number in array (0 until temp-1)
3- switch the fied that store largest with field indexed by (temp-1)
4- temp-- (decrement)
5- repeat steps 2 to 4 until temp is 1

the program then prints the numbers of array on screen in one line. I also should use a function getMax(int *list , int n) that determines largest value and returns its location in that array

list : is the array
n: number of elements

Code:
#include<stdio.h>
int getMax(int *list, int n); //definition of getMax function
int main(void) {
int n,i;

[Code] .....

so i tried to sort this array

Code:
input 2 7 9 4 3 1 6 5
The out put should be : 9 7 6 5 4 3 2 1
instead it prints : 7 9 2 4 3 1 6

View 11 Replies View Related

C/C++ :: Sort String In Map Comparator Function

Jan 19, 2014

i am currently using a comparator function in STL map for sorting date of string data type in ascending value. My dates are in this format for e.g. 15OCT1990, 13SEP1980 and etc. I am using substring to split up the string so that i can compare day, month, year separately. Right now i have problem comparing the month portion because alphabetically "FEB" comes before "JAN".How can I make a fixed substring position of (2,3) which is my month value to accept that string value of JAN comes before FEB,AUG,DEC for e.g?

struct sortMapDaily: public std::binary_function <bool, std::string, std::string>
{
bool operator() (const std::string& lhs, const std::string& rhs)
{

[Code].....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved