C++ :: Sorting Integers Without Arrays?
Jan 29, 2015
I have been tasked with sorting a text file with some numbers in it. For example, there can be 5 numbers in it: 1,2,3, 4, and 5. However, they are out of order (3,2,4,1 and 5). I need the numbers in numerical order. How can you sort the numbers into a new file?
Bubblesorting? I can not use arrays in this program. I have already determined the minimum number in the file.
View 5 Replies
ADVERTISEMENT
Feb 9, 2014
I am having some problem with my quick sort problem. My program is supposed to create 5 arrays with 5,10,15,and 20 random integers, respectively. Then it should sort those arrays, where the numbers are bigger or smaller than the middle element in the original array! The program I wrote should do that but, its not! The program just keeps running infinitely!
#include <iostream>
#include <cstdlib>
using namespace std;
void p(int k[],int left, int right) {
int i = left, j = right;
[Code] ....
View 8 Replies
View Related
Sep 21, 2014
Like I have input in range of 0-10^6. So first storing the integers (again in the range 0-10^6) and then sorting will certainly take a lot of time.
I was wondering whether we can sort on the go(like as the user is giving the data by standard input) and we are sorting it.
I don't want to use any standard library for this just yet (if there is any, how to point out). I am certainly baffled by this problem.
View 5 Replies
View Related
Jun 26, 2013
This program is sorting a randomized array of integers using the bubblesort algorithm.
I am trying to modify n correct the source code,so that the swapping of two values will be done by a function called swap values() by using call-by-reference but function should have as arguments only the two array elements that must be exchanged. (Note: do not pass the whole array to the function!) .We consider an array with the first element containing the number of elements in the array, followed by 10 randomly initialized integers (elements).
The code must sort the 10 elements in ascending order.
Compile: g++ -Wall sorting.cpp -o sorting
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
const int SIZE=10;
[Code] .....
View 1 Replies
View Related
Jul 31, 2014
I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?
View 1 Replies
View Related
Feb 1, 2015
I am relatively new to C++ and am trying to bubble sort my linked list that creates 100 random integers. Everything works, but I am unsure how to continue this to include a bubble sorting method.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node{
public:
int data; //set data
[Code] ....
View 1 Replies
View Related
Jul 21, 2013
So the question is to let the user put in two integers and put each integer in an array as separate numbers, and then sum these arrays together, so putting in the array is not so difficult, only problem i am having is first when the user have to input the first integer, he have to put space after each number, second then I am not sure how to sum them up, so if 45676 is first array and 56717 is second, we have to start summing them up from 6 and 7 (the last numbers). Here is what I did for now:
#include <iostream>
using namespace std;
int main () {
char Num1[5], Num2[5];
int i1; int i2;
cout << "input the first number" << endl;
for (i1=0; i1<5; i1++)
cin >> Num1[i1];
[Code] ....
View 3 Replies
View Related
Nov 21, 2013
I am trying sort my Calorie array and day array corresponding to each other but how to start i cant grasp the concept for some reason. I want it to sort by calories in descending order.
So instead of this...
Day: Calories:
1 200
2 500
3 400
4 600
5 100
I want this.....
5 100
1 200
3 400
2 500
4 600
#include <cstdlib>
#include <iostream>
using namespace std;
//Prototypes
int getDays(int);
void getCal(int*,int);
float calcSum(float,int*,int);
[Code] ....
View 1 Replies
View Related
Dec 9, 2014
Add a program that declares two arrays of 5 integers each. Provide one function to load one array with random numbers from 1 to 25, another to fill the other array with user supplied values, and a third function to print an array. The main function should declare the arrays and use the functions to fill each array and print them both.
The code i have so far is
#include <cstdlib>
#include <iostream>
using namespace std;
void printaaray(int printarray1[], int printarray2);
[Code]....
View 2 Replies
View Related
Mar 26, 2013
I wanted to sort two structure-arrays according to their element values and get their indices changed accordingly.For example if my array is
A[]= { 2 4 1 8 6}
indices[]={ 1 2 3 4 5}
then after sorting it should become
A[]= { 1 2 4 6 8}
indices[]={ 3 1 2 5 4}
I tried implementing it using the following code but I failed to get correct output.
Code:
#include<iostream>
#include<cstdio>
using namespace std;
#define inf 100000
struct array
{
int x;
int y;
};
[code]....
View 4 Replies
View Related
Apr 22, 2014
I am trying to create a program that will read data from a text file into a string. Afterwards I need to sort the data in ascending order and print it. So far my program reads the data but I'm not sure how to go about indexing the array.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[Code].....
View 3 Replies
View Related
Nov 7, 2014
I'm trying to write a program that should output, in a tabular form, the name of the algorithm, size of the array, number of comparisons and number of swaps. I am really, really, really bad at arrays and comprehending how to output variables from several. Especially in tabular form />
Here is what I have written thus far:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ARY_SIZE 10
[Code]......
My questions:
1 - How do I make the array size change from 10 to 100 to 1000?
2 - How do I populate the array's with random non-repeating numbers?
3 - How do I make each algorithm count the # of comparisons and swaps?
4 - How do I return the counted comparisons AND swaps (in one return function??) and then print them?
View 6 Replies
View Related
Feb 19, 2013
There is one question :
Considerint A[10]={ ....................}; // already filled
int B[10]={ ....................}; // already filled
Using PIONTER NOTATION ONLY, write a function that receives two arrays of integers like A and B above. The function should swap the values in A and B. You may NOT use array notation [ ]. Also, you have to use pointers to move among array cells. Note: Both arrays are of the same size, and size should be variable in the function.
View 3 Replies
View Related
Jul 11, 2013
Having trouble with homework involving (title). Here is the assignment:Write a program that allows the user to enter 10 numbers from the keyboard. Sort the numbers using any sort routine you wish. The output from your program should be 2 columns of numbers. The left column should be the numbers in the order they were originally entered and the right column should be the sorted list. The columns should be labeled. You will need 2 arrays to accomplish this.
Use separate functions for input, sorting, and printing.So, I have to use separate functions for each of these. I would think it would be easiest to do the input in Main() and then the sorting and printing in another function, but of course since you can't return arrays I am kind of stuck on how I return the new array after sorting. I thought about doing the sorting in Main(), but then I would need to still do the original arrays input in a function and would still run into the same problem.
Here is what I have so far:
Code:
//Cameron Taylor
#include <stdio.h>
#define MAXARRAY 10
int highLow(int[], int);
int print(int[], int[], int);
int main (){
int unsorted[MAXARRAY], i, j, temp;
[Code]...
I know it seems simplistic right now, but I am just trying to get it to work first and then go back and beautify it up a bit.
View 9 Replies
View Related
Jan 10, 2015
Write a program using user-defined function to merge the contents of two sorted arrays A & B into third array C. Assuming array A is sorted in ascending order, B is sorted in descending order, the resultant array is required to be in ascending order.
View 1 Replies
View Related
May 28, 2014
// OK this program outputs an array of numbers which are read from two .txt //files which are set1 and set2.
// set1.txt has: 8 37 29 31 40 25 18 38 4 45 34 39 12 21 24 3 5 23 26 44
// set2.txt has: 46 42 25 20 19 29 49 4 32 2 10 12 39 17 33 6 3 15 45 21
// But the problem is that when you run the program, the numbers do not come out // in numerical order.
#include <iostream>
#include <fstream>
#include <string>
[Code]....
View 2 Replies
View Related
Apr 2, 2014
Write a function to read and display the contents of names and marks. You then ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
Next, sort the arrays, write them out and then ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student's name and mark if found.
void getNames(ifstream& inStream, string names[], int marks[], int numElts);
int linearSearch(const string names[], int numElts,string who);
int binarySearch(const string names[], int numElts,string who);
void selectionSort(string names[], int marks[],int numElts);
void displayData(const string names[], const int marks[], int numElts);
[Code] ....
Now I have worked up some stuff in parts but I am so lost and confused with these specific requirements: Previous questions asked me to sort out a linear search, a binary search and
LINEAR SEARCH:
int searchList(int list[], int numElems, int value) {
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if value was found
[Code] ....
View 3 Replies
View Related
Apr 4, 2014
Class programming project where we declare two arrays, a sting array containing the names of the boroughs and an int array which which holds accident number for each borough. The main function calls getNumAccidents() function to fill the array. it then calls findLowest() to learn which borough had the fewest accidents. It should print out the accident numbers for each borough and then out the borough and accident number with the fewest accidents.
I'm able to get the program to kind of work. When I run it everything works fine but I am not able to get the arrays to sort and match up correctly..
#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;
class combineSort {
public:
combineSort() {
[code]...
View 14 Replies
View Related
Oct 29, 2014
For my program i need to sort my int numbers [5] from highest to lowest but it seems that i made it go from lowest to highest ...
Code:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {3, -6, 10, 1, 130};
int counter1, counter2, tempNum;
//Highest to Lowest
[Code] ....
View 1 Replies
View Related
Nov 7, 2014
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code:
matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//This function checks if two matrices are equal
int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix
int * getRow(int **A, int i, int n);
//This function returns one column of a matrix
int * getCol(int **A, int j, int n);
[Code] ....
View 2 Replies
View Related
Feb 11, 2013
Code:
typedef struct {
name_t name;
float beta[beta_num];
} gene_t;
[Code] ....
Is gene an array of address ? How come the compare function doesn't work ?
View 1 Replies
View Related
Jul 30, 2014
In pseudocode and in C code, declare two 1-dimensional parallel arrays of Integers to store the age and weight of a group of people that will hold the information of up to 10 people. Use a For loop to iterate through the array and input the values. This is the code i have, but my array is showing up as 11 and not 10?
Code:
#include <stdio.h>
int main() {
//Define Variables
float Age_Data[10],Weight_Data[10];
float Age_Input,Weight_Input;
[Code] .....
View 3 Replies
View Related
May 7, 2013
I previously tried to put strings in an array, but i couldn't modify the first index in the string that is in the array. I changed my code later.
[URL]
Suggest a function that sorts the inputs vertically according to the order given by the user? I made this one
void order(string order, char index[][15]){
int counter=0,line=0,i=0,j=0;
for(i=0;i<=15-1;i++)
{for(j=0;j<=15-1;j++) {
if(index[j][i]==order[counter])
[code].....
View 1 Replies
View Related
Apr 18, 2013
I am trying to create a code to sort an array of integer, but only between two positions of the array, not the all array.
like this:
array: 1 2 5 4 7 2 9 8
index: 0 1 2 3 4 5 6 7
i want to sort the array per exemple between the the index 2 and 5.the result is... array: 1 2 2 4 5 7 9 8
View 2 Replies
View Related
Apr 21, 2014
I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?
View 2 Replies
View Related
Feb 6, 2014
I define "Comwords" as a string, but apparently it takes the members as chars, then I can't set strings in a structure equal to the chars.
I see to also be having unknown problems with the ComMAL array and loading it values into another element of the same structure.
How to correct this? I was thinking of casting char elements as strings, but could find no reference in my library book regarding how to do that (lots on casting int's a doubles...)
Code:
int _tmain(int argc, _TCHAR* argv[]) {
int comm = 10;
int targ = 5;
int death;
struct AI_WORDS
[Code]....
View 2 Replies
View Related