NEED ASSISTANCE W/ PROGRAM!!

1. Generate 10000 positive numbers between(0,200)
2. Search the position of the first 100 in the array
3. Count how many 100 in the array
4. Sort The array

CAN SOMEONE PLEASE ASSIST ME!! I'm lost....The program can be written in any language of choice but idk where how to start.
If you choose C++ start from here: http://www.cplusplus.com/doc/tutorial/
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>
#include <vector>
#include <time.h>

const int limit = 10000;

int randomNumber()
{
    return (rand() % 200);
}

int main()
{
    srand ( (unsigned int) time(NULL) );

    std::vector<int> list(limit); 
    
    std::generate_n(list.begin(), limit, randomNumber);

    std::find (list.begin(), list.end(), 100);

    std::count(list.begin(), list.end(), 100);
    
    std::sort (list.begin(), list.end());
    
    return 0;
}
OP Got lucky :D
What a poorly phrased assignment.
Generate 10000 positive numbers between(0,200)
That's both redundant (I don't suppose you can generate negative numbers in the range (0;200)) and unspecific: std::fill(list.begin(),list.end(),1);
Yeah I agree it's poorly phrased. To add on to this assignment it's supposed to be based upon the Quick Sort method. How would I insert the quick sort method into this. This is what i've written so far:

import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class CC
{
public static void main(String[] args)
{
int[] List = new int[10000];
int key = 100;
int sum = 0;
int index = -1;
int add = 0;
long StartTime = System.currentTimeMillis();

// Creates an array of 10,000 random positive numbers.
for (int l=0; l<List.length; l++)
{
List[l] = (int)(Math.random()*201);
}

long StopTime = System.currentTimeMillis();
long RunTime = StopTime - StartTime;
System.out.println("The time to process an array of 10,000 elements is " +RunTime+ " milliseconds.");

StartTime = System.currentTimeMillis();

// Returns the index of the first index containing the number 100
for (int l=0; l<List.length; l++)
{
if(List[l] == key)
{
if(index<0)
{
index = l;
System.out.println("The index of the first number 100 in the array is " +l);
}
}
}

StopTime = System.currentTimeMillis();
RunTime = StopTime - StartTime;
System.out.println("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+ " milliseconds.");

StartTime = System.currentTimeMillis();

// Computes the number of times the number of 100 is in the array.
for (int l=0; l<List.length; l++)
{
if(List[l] == key)
sum++;
}

StopTime = System.currentTimeMillis();
RunTime = StopTime - StartTime;
System.out.println("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+" milliseconds.");

StartTime = System.currentTimeMillis();

// Sorts the array using selection sort.
for (int i = List.length-1; i>=1; i--)
{
int cMax=List[0];
int cMaxIndex=0;


for (int j=1; j<=i; j++)
{
if (cMax<List[j])
{
cMax= List[j];
cMaxIndex=j;
}
}

if (cMaxIndex !=i)
{
List[cMaxIndex] = List [i];
List[i]=cMax;
}
}
/*for (int i=0; i<List.length; i++)
{
System.out.print(List[i] + " ");
}*/

System.out.println("There are " +sum+ " instances of the number 100 in the array.");

StopTime = System.currentTimeMillis();
RunTime = StopTime - StartTime;
System.out.println("The time to sort an array of 10,000 elements using Selection Sort is " +RunTime+" milliseconds.");

}
}
Did I just see me some Java?
this is the code that i've written in C++. Im in need of assistance in inserting the quick sort where i've got the selection sort at:

#include <string>

using namespace javax::swing;
using javax::swing::Timer;

class CC
{
static void main(std::string args[])
{
int List[10000];
int key = 100;
int sum = 0;
int index = -1;
int add = 0;
long long StartTime = System::currentTimeMillis();

// Creates an array of 10,000 random positive numbers.
for (int l=0; l<sizeof(List) / sizeof(List[0]); l++)
{
List[l] = static_cast<int>(Math::random()*201);
}

long long StopTime = System::currentTimeMillis();
long long RunTime = StopTime - StartTime;
puts("The time to process an array of 10,000 elements is " +RunTime+ " milliseconds.");

StartTime = System::currentTimeMillis();

// Returns the index of the first index containing the number 100
for (int l=0; l<sizeof(List) / sizeof(List[0]); l++)
{
if(List[l] == key)
{
if(index<0)
{
index = l;
puts("The index of the first number 100 in the array is " +l);
}
}
}

StopTime = System::currentTimeMillis();
RunTime = StopTime - StartTime;
puts("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+ " milliseconds.");

StartTime = System::currentTimeMillis();

// Computes the number of times the number of 100 is in the array.
for (int l=0; l<sizeof(List) / sizeof(List[0]); l++)
{
if(List[l] == key)
sum++;
}

StopTime = System::currentTimeMillis();
RunTime = StopTime - StartTime;
puts("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+" milliseconds.");

StartTime = System::currentTimeMillis();

// Sorts the array using selection sort.
for (int i = sizeof(List) / sizeof(List[0])-1; i>=1; i--)
{
int cMax=List[0];
int cMaxIndex=0;


for (int j=1; j<=i; j++)
{
if (cMax<List[j])
{
cMax= List[j];
cMaxIndex=j;
}
}

if (cMaxIndex !=i)
{
List[cMaxIndex] = List [i];
List[i]=cMax;
}
}
// for (int i=0; i<List.length; i++)
// {
// System.out.print(List[i] + " ");
// }

puts("There are " +sum+ " instances of the number 100 in the array.");

StopTime = System::currentTimeMillis();
RunTime = StopTime - StartTime;
puts("The time to sort an array of 10,000 elements using Selection Sort is " +RunTime+" milliseconds.");

}
};
Topic archived. No new replies allowed.