C++ :: Finding Second Smallest Element In Unsorted Array
May 5, 2013
How should I go about finding the 2nd, or 3rd smallest element in an array? Where the minimum is not equals to the next minimum.
Here is the psuedocode I've found
void Min(A[i..j], int&min, int &next_min)
if (i=j) then min=A[i] and next_min = infinity
else
mid = (i+j)/2
Min(A[i to mid]),a,b)
Min(A[mid+1 to j],c,d)
Let min be minimum among a,b,c,d
Let next_min be the next_min among a,b,c,d
min != next_min
Here is what I wrote but it does not seem to work.
#include<iostream>
#include<vector>
#include <cstdlib>
using namespace std;
void minfind(vector<int> &array,int left,int right, int &min, int &next_min);
[Code]...
View 5 Replies
ADVERTISEMENT
Oct 17, 2014
I was assigned to create a program that has a 10 x 8 two dimensional array and fill it with random numbers in the range 50 to 70 inclusively.( Got That part down). The second part is to make function named findSmallest that will find and print, appropriately labeled, the smallest number in the array.
I cant seem to get it working. The code for finding the smallest give me some weird number
Here my Code:
//Assignment 18 Program 2
#include <iostream>
using namespace std;
[Code].....
View 2 Replies
View Related
Feb 17, 2014
"Write a program in C that finds the element of an array which all the previous are smaller and all the next are bigger than that.If there are more than one print the biggest. If there isn't any print "ERROR" .
EXAMPLE
If the array has 10 elements like this : {2,3,4,6,5,7,8,10,9,8}
The correct answer is 7 , because 2,3,4,6,5 are smaller than 7 and 8,10,9,8 are bigger than 7.
I am working on it for 2 weeks and I can't find a working solution />/>/>
There is the first try :
#include <stdio.h>
#include <stdbool.h>
int s_data[10]={2,3,4,6,5,7,8,10,9,8};
int main() {
int result,i,j,s_len ,tmp1,tmp;
bool GT_PREV,ST_NEXT;
[Code] .....
View 11 Replies
View Related
Mar 12, 2014
Above you said to find the middle value you should divide the array by two and then display the middle value (aka the median). For example, for an array consisting of 1 2 3 4 5, 3 would be the middle value.
My question is, how/what do I write to tell the program to display the middle value???
So far, I have done enough research and written enough code to receive values from the user and sort the array in ascending order. I simply don't know where to go from here (I started C++ about 1 month ago). I don't know how to tell it to pull the middle value.
I've researched "find_if", "nth_value" and a boat load of others with no luck
Here's what I've done so far:
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
[Code]....
View 3 Replies
View Related
Oct 7, 2014
This is for a 2-d array, but if i understand how to do it for a 1-d array, I can do it for a 2-d. So to keep it simpler, I will just pretend its for a 1-d array. I am working on a project where i must enter the month number of a storm and then tell the user which month had the most storms. So I have an array and the user determines the size of it by entering the number of storms. Then, using a for loop, the user enters the month number for each storm and the data is used to fill the array. This is to be used in a part of a much larger piece of code. I made a sample code below to mimic what i want to do:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
/*Declare variables, i is a counting index used in for loop if necessary*/
[Code] ....
I believe this involves making some temporary array but where to start ....
View 2 Replies
View Related
Mar 13, 2013
This code, asking to mark both the smallest and the largest element. I just got largest, didn't get smallest number. Here is code:
#include <iostream>
using namespace std;
int main() {
const int CAPACITY = 1000;
double values[CAPACITY];
int current_size = 0;
[Code] .....
View 9 Replies
View Related
Feb 9, 2013
I'm trying to write a program that will allow a user to enter infinite numbers (one at a time) with a way to stop entering values, and once it stops it immediately displays:
Lowest number:
Highest number:
Number of values entered:
Average of numbers:
what I can't figure out is:
-how to end the loop with something other than a number, preferably a char, so that it doesn't affect the average/lowest/highest
-how to output the lowest and highest numbers in a loop (would be easier to figure out were it finite and I could just type a bunch of printf/scanf)
What I have so far:
Code:
#include <stdio.h>#include <stdlib.h>
#define pause system("pause")
main(){
//n = amount of numbers input
//x = number given by user
//s = smallest number
//l = largest number
}
[code].....
View 4 Replies
View Related
Sep 29, 2014
I've been working at this for awhile and it seems like it should be correct but it isn't giving me the desired output. Here's my code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
[Code]....
And here is my output when I run the program.
75
Max: 90
Min: 0
Press any key to continue . . I am reading from a file in this program which contains a set of 30 numbers and it has the average correct and the max correct but the minimum number in the set is 56 however it keeps giving me a zero for the min.
View 4 Replies
View Related
Sep 10, 2013
I am stuck looking at whats the problem ....
#include <iostream>
using namespace std;
int main() {
float average, largest = 0, smallest = 0;
int count = 1;
int num;
[Code] .....
View 1 Replies
View Related
Oct 21, 2014
I need to generate a grid of 10x8 and fill it with random numbers (max 70), then i need to find the smallest number within the random numbers generated and my "findSmallest" function does not seem to work and i do not know how to make it work...
Here my Code:
include <iostream>
using namespace std;
int main() {
int row=0;
int col=0;
int ArrayGrid [9][11];
srand(time(NULL));
[Code] .....
View 4 Replies
View Related
May 25, 2014
Write a program that reads in a list of integers into an array with base type of int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.
For example, for the input
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
the output should be
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
Here's My code So far:
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE = 50;
[Code]....
My Code outputs the numbers From Largest to Smallest according to given array, but I need to make it to output the numbers once(if its repeated)
View 2 Replies
View Related
Jul 17, 2012
When I run my program it just gives me 'The smallest number is: 2682760' which isn't true obviously.
Code:
#include <iostream>
int main() {
using namespace std;
int x, smallest;
int Array[7] = {7, 5, 10, 13, 3, 15, 54};
x = Array[0];
[Code] .....
View 3 Replies
View Related
Feb 16, 2015
i'm having trouble trying to make this code works .. the program must repeatedly asks user for the age of a person until user enters -1 ,, once he/she entered -1 the program calculates ( smallest value entered, largest value , and average )excluding -1 from calculation
i've been working on this for a weeks this is the best i could do.i know average must add all the entered values , not only smallest and largest but how can i do that here ?
Code:
#include<stdio.h>
int main(void)
{
int age ;
int v;
int counter;
int l=0;
int s=0;
int limit;
}
[code]....
View 13 Replies
View Related
Dec 4, 2013
I am working on a c++ assignment and I need to display the smallest and largest values of an array called from a .txt file...but I don't know how to display them...
This is what I have so far::
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
[Code].....
and the .txt file is:
12 43 11 10 9 54 36 75 96 83 23 35 67 35 47 58 69 13
45 67 89 78 66 44 1 55 59 81 18 0 42 199 4 100 46 76
View 2 Replies
View Related
Mar 16, 2014
I'm trying to sort the numbers in an array from largest to smallest and print it but whenever I run this it does not sort anything at all.
#include <stdio.h>
#define SIZE 10
void Input(const int array1[]);
void Calculations(int array1[], int average);
void Output(int array1[], int average);
size_t counter1 = 0;
int average;
int main( void ) {
int array1[ SIZE ];
[code]....
View 4 Replies
View Related
Oct 24, 2014
I need to find the smallest number in my 10x8 arraygrid with random numebr filled in it
Here my Code:
#include <iostream>
using namespace std;
int main() {
int total,average,smallest;
int row=0;
int col=0;
[Code] ....
View 2 Replies
View Related
May 13, 2014
im trying to write a source code that find the smallest, largest and average of numbers in array. the code runs fine, but it is not giving the highest number and the the average should include only four number excluding highest and smallest number from the array.
void OlympicJudging() // Olympic Judging {
int numbers [6];
double average, sum = 0;
int temp;
for(int i = 0; i < 6; i++){
cout << "Please type a value for scores: ";
cin >> numbers[i];
[Code]...
View 5 Replies
View Related
Mar 10, 2014
So I have linked list and function which deletes element if next element is bigger, so my code is working but its not working with first element, in the comment I have wrote code which I would code for checking that first element, but when ever I check it is blowing up all program.
#include <iostream>
using namespace std;
struct llist {
int x;
llist *next;
[Code] .....
View 1 Replies
View Related
Oct 5, 2014
My code has been acting odd. I made a function that layers my art resources but only the last tile of my art resource acts the way it should. My character goes behind and in front of the last tile and gets printed correctly. Strangely its really exclusive to the last tiles I print. What I need is to figure out in step by step order what going on with my code sample below and be able to layer the resources.
Here is a small sample of my main function. This is how I do my rendering.
Code:
Int main (int arc, char* args[]) {
//Move class
Move character;
//Class Tile & Side Tile
Tile *tiles [TOTAL_TILES];
[Code] ......
View 14 Replies
View Related
Mar 6, 2015
how to compare each element of array with another,defined ? I have tried this,but didn't work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bill()
[Code].....
View 3 Replies
View Related
Jan 27, 2013
I want to a C program to delete an element from an array which can use both index method & character method
For example
input : "1 222 333 4444"
output:"1 22 333 4444"
if either index = "4" or character ="2" is entered
It should able to read in/accept any input array of varying length, and the position/index or element in array to be deleted...
View 6 Replies
View Related
Jan 9, 2015
how to delete an element(s) from an array; suppose I have an array x[10] = {1,2,3,4,5,6,7,8,9,10}, and I want to delete array{5} so that the values of the array become {1,2,3,4,5,7,8,9,10}; how do I go about this? This is not the same as setting the value of array{5} to null; but completely eliminating it so that it does not get printed alongside the other elements of the screen.
View 3 Replies
View Related
Oct 7, 2013
Lets assume, I use array of 100 the i input 50 element and now i want to find which one is first in array... without using pointer ...
View 2 Replies
View Related
May 6, 2014
This is a program to get an 10 characters including a-z, A-Z, and _ .
Then this program will cout the text with out _ !
Example:
cin >> sd_fd_e_dd
cout << sdfdedd
# include <iostream>
#inclued<vector>
using namespace std;
char a[10],m;
[Code] ....
View 1 Replies
View Related
May 13, 2013
How would I go about having a pointer to an array element specificity a character in a c-string.Every thing I try will not even build.An array is already a pointer to the first location of the array right?
char *pHead;
char *pTail;
pHead = sentence[0]; <=== This wont build
pHead = &sentence[0];
pHead = sentence[0]*;
*pHead = sentence[0]; <===== this builds but is not storing anything
View 5 Replies
View Related
Feb 28, 2013
I am beginner in C++ programming. I having a problem to show the output of all the element i store at a array called total_price. the output become unreadable.
#include <iostream>
using namespace std;
int main () {
float price[1]={0};
int qty;
[Code] ....
View 6 Replies
View Related