C :: Sorting Array Of Integers Between Two Positions Of Array
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
ADVERTISEMENT
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
Feb 12, 2015
I have a fundamental question due to my lack of understanding how arrays work.Let say I have a character array (byte array).
1. do array elements have memory adresses? i guess not but ....
2. if yes is it possible to switch two array positions? For examle:
a[x] change with a[y] for x,y < |a|
as oppose to changing values assigned to individual array positions.
View 2 Replies
View Related
Feb 11, 2013
For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.
So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time (NULL));
[Code] ....
View 3 Replies
View Related
Apr 8, 2013
I want to rearrange the positions of structure elements on the bases of perm_array.
Code:
typedef struct gg{
element d;
int group;
} gg;
gg col_data[16];
[Code] ....
How can i rearrange structure element like {ptr+2,ptr+3,ptr+1,ptr+0} ?
View 2 Replies
View Related
Sep 27, 2013
I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :
1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
"
View 7 Replies
View Related
Jun 7, 2014
Objective: Write a function with the given signature that will take a sorted array of integers and return the array compacted. That is, given an array containing: 1, 2, 6, 8, 8, 8, 9, 10, 10, when the function returns, the contents of the array should be: 1, 2, 6, 8, 9, 10.
Restrictions:
Cannot use Distinct method
Signature:
public static int[] CompactArray(int[] input)
View 14 Replies
View Related
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
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
Mar 21, 2014
I now know how to count integers with while loop but I'm not sure how to count the integers with array.
So the question is:
1. program should keep reading integers as long as the integers are within [0,9999]
2. when user typed the integer not between 0 to 9999, the program print out the numbers of integers that were typed.
Sample
3
3
3
9999
9999
-1
You entered 3 3 times.
You entered 9999 2 times.
#include <iostream>
using namespace std;
int main() {
int i=-1;
int x;
int numbers[10000];
[Code] ....
I cannot use "do" ....
View 1 Replies
View Related
Feb 28, 2013
How to solve this because i was not able to get the idea of using arrays in classes . Here is the Question :
The PVR Cinemas manager approaches you to develop a system for ticket booking. It has 5 screens each with a capacity of 500 seats of which 100 are platinum, 100 are diamond and 300 are gold. The cost of platinum, diamond and gold tickets are Rs.150, Rs.125, and Rs.100 respectively.
•Construct a class to model a screen with an array of integers for each category of seats. Provide a constructor to initialize the array to 0.
•Include a member function bookSeat() that gets the category of the seat and number of tickets to be booked and then prints the seat numbers. The booked seats are marked by 1. If no seats are available then your program should display appropriate message.
Test the above class with a main program that creates an array of objects (size of array depends on the number of screens) and display the total amount to be paid by the visitor.
View 6 Replies
View Related
Jan 30, 2013
I am creating a program and am having trouble with one part. The input file looks like:
Code:
2
3
Bill Jeff Steve
Linda Brittany Jessica
3 1 4
2 1 9
8 3 7
6 4 9
4 8 9
6 9 4
Where I am stuck is putting the numbers into the string array. For example, Bill's scores should be set up as Bill's scores should be set as
Code: bill_score = {3, 1, 4}
and when this code is ran
Code:
for (i=0; i<3; i++) {
printf("%s - ", men[i]);
[Code].....
View 4 Replies
View Related
Jan 14, 2013
I am trying to find a way to do something like this:
input: 3 4 7 4 3 3 7
output: 3 4 7
So what I am trying to do is from an array of integers to take numbers that occur only once. If 3 is in that away I am trying to input it in a different array only once.
input: 8 3 2 9 8 9 2
output: 2 3 8 9
I cannot find a way to solve this, and I have been trying to solve it for a long time.
View 8 Replies
View Related
Mar 4, 2013
here is my problem given below Input values (say 10) from user in array, if the value is even then place at even index else at odd index. Then how could i solve this problem?
View 2 Replies
View Related
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
Feb 8, 2015
I tried sorting arr2 from lowest to highest value, but it only gives me 4 values and then the rest are zero.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(1);
[code].....
View 1 Replies
View Related
Feb 13, 2013
Selection sorting a 2D array . Let's say i have an array like
1 2 3 4 //4 elements
1 2 // 2 elements
1 2 3 4 5 //5 elements
1 2 3 //3 elements
1 //1 element
And I want to do a selection sort it in descending order which the row with 5 elements will come first then 4 then 3 and so on. So that it would look like this
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Code:
void selectionSortDescending(int list[MAX_ROW], int size){
int temp;
int walk;
int curr;
int large; // index of the largest element
for (walk = 0; walk < size - 1; walk++)
[Code] ....
View 7 Replies
View Related
Nov 18, 2013
I am trying to sort an array of size 10. If I was given:
Code: int List[Size] = {29, 11,12,10,3,26,13,15,19,2};
I need the program to sort all the odd integers and put the even integers to the back of the array. Like so:
Index: 0 1 2 3 4 5 6 7 8 9
Value:29 11 3 13 15 19 12 10 26 2
And it returns the number of even integers in the List. In this case it returns 4. All I am given to start with is
Code:
int evensToRead(int* const List, const int Size){
//body
}
View 1 Replies
View Related
Oct 24, 2013
i have a matrix containing a lot of points and each point has its coordinates x and y. That is a nx2 size array. I want to sort it according to the first column ascending, with x coordinates. For points that have the same x coord i would like to sort according to y coord. Here is what i did and i cannot get a good result.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int a[5][2] = {{1,0}, {4,2}, {2,4}, {8,6},{4,8}};
int temp=0;
int i=0;
int j=0;
[Code]...
View 4 Replies
View Related
Jan 22, 2013
i need to print the names as they appear in the original file, print the info of the person with the highest distance, print the info sorted by ascending ID number, and print sorted by name alphabetically. the first two parts work fine but sorting by ID and Name dont work.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
[code]....
View 1 Replies
View Related
Jan 3, 2014
I have a question about sorting a 2D array. Lets suppose I've got the following array:
2 5 7 4 8
3 11 14 5 2
6 3 12 9 1
7 15 11 4 2
8 16 13 5 1
I would like to sort this array diagonally to make it look as : [URL] ....
View 10 Replies
View Related
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
Jan 3, 2014
How to sort a 2D array diagonally? Lets suppose I've got the following array:
2 5 7 4 8
3 11 14 5 2
6 3 12 9 1
7 15 11 4 2
8 16 13 5 1
I want to create a function that sort them diagonally like this: [URL] .....
View 7 Replies
View Related
Mar 3, 2014
For my code, I want the user to enter 4 integers, which get stored in an array. Here are the lines I wrote relevant to this part:
Code:
printf("Enter 4 integers:
");
scanf("%d %d %d %d," &a, &b, &c, &d);
z[4] = { a, b, c, d};
Would this be correct?
View 5 Replies
View Related
Jun 22, 2013
Suppose I wished to initialize a dynamically allocated array of integers to zero. Would I do better to use calloc() or malloc + iterate over all entries setting each to zero? Which one is regarded as a better approach?
View 12 Replies
View Related
Mar 2, 2014
I know strings are essentially just arrays of characters, so what would be the easiest way to take each individual digit and put it into a separate space in an array?
ex.) *str = "90210"
array[0] = 9
array[1] = 0
array[2] = 2
array[3] = 1
array[4] = 0
All my attempts at achieving this just result in an array full of garbage numbers. What I've done is
Code:
int *array;
array = malloc(sizeof(int)*(strlen(str));
for(i=0; i<strlen(str); i++) {
array[i] = str[i]
}
also, I should mention that the string will be defined in main, and its converted into an array in a separate function.
View 2 Replies
View Related