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


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 :: 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 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++ :: Concatenate Two 2-dimensional Int Arrays Into One Larger 3-dimensional Array

Jul 31, 2013

How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:

std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());

and for the one dimensional array as:

int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

But I do not know how to make a 3-dimensional array or vector.

View 3 Replies View Related

C++ :: Converting One-dimensional To Two-dimensional Array

Jan 17, 2014

I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.

[code]....

I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!

View 2 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 :: Assigning One Dimensional Array To Two Dimensional Array

Mar 19, 2014

is it allowed to to like this:

char a[10] = "Lizard";
char b[2][5];
b[0][0] = a[0];
b[0][1] = a[1]; etc?

View 1 Replies View Related

C :: How To Copy 3 Dimensional Array Into 2 Dimensional Array

Sep 2, 2013

I have a 3D array that contains 200 strings. I'm trying to copy all these strings into a 2D array. How can this be done? This is what I have so far but it isn't working correctly.

Code:
for(int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
dest[i][j] = source[0][i][j];
} }

The finished product would be with 100 rows, 2 columns.

View 4 Replies View Related

C :: Converting One Dimensional Array To Two Dimensional Array

Aug 30, 2013

convert an one dimensional array into a two dimensional array and print like a matrix.

input: 34 50 2 4 90 33 7 80 9
output: A is a 3x3 matrix
34 50 2
4 90 33
7 80 9

View 12 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++ :: Accept Integer Array And Its Size As Arguments And Assign Elements Into 2 Dimensional Array

Jan 10, 2015

Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is

1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0

View 1 Replies View Related

C :: 2 Dimensional Array Max And Min

Apr 20, 2013

I was able to get the max to work but I cant get the min. it returnning something like -125863456.

Code:
int maxx(int a[][10]);
int minn(int a[][10]);
int main(void){
int array[][10] = { { 2, 7, 6, 8, 4}, {3, 9, 1, 5, 6} };
int max1, min1;

[Code] .....

View 3 Replies View Related

C++ :: 3 Dimensional Array Printing?

Oct 25, 2013

I want to make a program that asks the user for a message and then print out a large graphic of that message. For example, if the user types "he" I want to print out

H..................H EEEEEEEEE
H..................H E
H..................H E
H..................H E
HHHHHHHHHH EEEEEEEEE
H..................H E
H..................H E
H..................H E
H..................H EEEEEEEEE

(treat the periods as spaces. I only put them there because it wouldn't separate the H's correctly.)

I will loop this to continue until the user types quit.

1. How would I set this up to store the user input characters into an array?

2. How would I print out the stored data in the shape of the word?

View 4 Replies View Related

C :: How To Create 8 Dimensional Array

Dec 19, 2014

Is it possible to create an 8 dimensional array in C? If no, then what can I do to improvise or let say bypass the limit?

View 7 Replies View Related

C++ ::  Deleting Two Dimensional Array

Oct 21, 2014

Consider the following code snippet:

GLfloat box[4][4] = {
{ x2, -y2, 0, 0 },
{ x2 + w, -y2, 1, 0 },
{ x2, -y2 - h, 0, 1 },
{ x2 + w, -y2 - h, 1, 1 },
};

Do I need to call a variant of

delete [] box;

Against this float array?

View 1 Replies View Related

C++ :: Two Dimensional Array With Pointer?

Apr 1, 2013

int sum(int (*ar2)[4], int size);

// I dont know what the ar2 is going on

int main(){
int data[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int total = sum(data, 3)
return 0;
}

View 4 Replies View Related

C# :: Sorting 2 Dimensional Array By Value

Jan 9, 2014

Here is what I have, I have a 1D Array being added to a 2D Array and I need to Sort them by value value 3 in the 2D Array, while maintaining a specific amount. Here is what I have so far:

public static void CheckHS(string[] HS) {
try {
GeneralData.HighScores[10, 0] = "11";
GeneralData.HighScores[10, 1] = HS[1];
GeneralData.HighScores[10, 2] = HS[2];
GeneralData.HighScores[10, 3] = HS[3];
//Need Sort Data - Bubble Sort?
}//end Try
catch (Exception ex) { MessageBox.Show(ex.Message); }
}

I am thinking bubble sorting but I remember reading about something faster. Unfortunately I can't find it on the web. The idea is that there will be always 10 Values and 4 Columns on the 2D Array. [The 11th Row being empty at the end of it.

View 14 Replies View Related

C/C++ :: How To Create 4 Dimensional Array

Apr 24, 2013

I want to create 4 dimensional array, in that first three dimenstional are fixed size and the final index will be on 0 to N-numbers.

E.g., double array[500][25][10][<NOT FIXED>].. So I cant create statically, because the index size are more. Also I have tried 4 dimenstional vector, but its giving 2 problem.

(i) I am storing 4th dimenstion size is more than vector[0][0][0].max_size()
(ii) Storing and Retrieving its more time in vector

So, any other solution to store large array which is 3 index is FIXED and final one is not FIXED?

View 16 Replies View Related

C++ :: Sparse 2 Dimensional Array?

Feb 26, 2012

i created a program which uses Sparse 2 dimensional array, but i am not sure if i did it in the right way .

this is the instruction i have:

Create a constructor and a destructor. The constructor should take as input the size of the array (consider only square NxN arrays, so only one dimension is needed) and the thickness of the ribbon. To make this precise, if supplied with a thickness parameter t, you may assume that the element [0,t] (i.e. the (t+1)-th element of the first row) is where the useless area begins on the right. Similarly, the element [t,0] is where the useless area begins on the left. The border of the useless areas moves diagonally down and to the right, i.e. it consists of [1,t+1],[2,t+2],... and [t+1,1],[t+2,2],... The above example has thickness 3.

The space for the 2-d array should be dynamically allocated and must be large enough to fit the useful data only.Create methods for random read and write access to the array as in the case of 1-d arrays.Overload the [] and << operators, as in the case of 1-d arrays. Think carefully about what the [] operator should return and how it should work. Ideally we would like this to behave in a manner similar to standard 2-d arrays (i.e. accessing elements in the normal way, like x[5][6]).

View 3 Replies View Related

C++ :: Adding Up Elements Of Two Dimensional Array?

Jan 9, 2015

This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:

Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()

[Code]....

Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:

1 2 3 4 5
6 7 8 9 10

I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:

7 9 11 13 15

View 8 Replies View Related

C :: In Two Dimensional Array 0,0 Spot Keeps Changing

Jan 12, 2015

I have a 10x10 array, initialized to all zeros. I create 2 random numbers for the purpose of guessing a position in the array.
However, when I print the array, the 0,0 spot keeps shifting to match the 2nd random number generated. Is there something strange about the 0,0 spot?

Here is my code:

Code:
seedrnd();
for (x=0;x<2;x++) {
randArray[x]=rnd(10);
}
for (x=0;x<2;x=x+1)

[Code] .....

The 2nd to last print statement actually prints randColumn. The last print statement correctly prints 0.

View 7 Replies View Related







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