a little help please

Pages: 123
Hello everyone, I have a little request, I know that many of you frown upon such things, but..I have nowhere else to turn, in short: if I don't pass this test I'm not getting my high-school diploma tomorrow, please I beg you to help me solve this
"write a program that will format true random sequence a0, a1, ..... a100n, values ​​[-A, B] segment, where A = k, B = 2 * k * n, k-ticket number, n - your number. sort descending Order obtained by using a simple insertion algorithm to classify moving and print largest 2nd element."
Thank you.
Last edited on
am0n wrote:
if I don't pass this test I'm not getting my high-school diploma tomorrow

You should have studied and practiced more. The real world is not going to be so nice. If you can't do the job or even put forth effort, then you're fired. Nobody is going to do this for you. We'll be glad to help, but you have to show us work that you have already done with this.

EDIT:
It's also weird that a high school student needs a programming class to graduate. Is that part of the required curriculum now??
Last edited on
Thank you for you reply, I'm all in for even slightest help, the truth is I'm not planning to pursue my career as a programmer, but yes you're correct I should have studied..
closed account (3qX21hU5)
Weird didn't know high school kids graduated this early lol its barely spring.

Anyways show us what you have so far, and we will work WITH YOU to figure it out. We won't just do it for it so if that is what your asking you might as well just forget it.

So post what you have so far and where you are stuck at, what problems you have run into, what you have tried, anything you don't understand, ect. Also remember to use codetags (Highlight code then press the <> under format) when posting your code.

Then we can help guide you to getting the problem solved.
Yeah I live in a foreign country more specifically Georgia where we graduate around this time(well a month later, but testing is tomorrow)
So far I have NOTHING, but that is not due to me being lazy, I just have no idea where to begin the most complex thing I can do in C++ in print out odd and even numbers and do a basic loop.
Interesting, I had no idea there was a country called Georgia.

Anyways just do it on paper first. Write out the logical steps that have to happen for this to work. Don't even really think about programming yet, just think about the problem as a series of steps.
closed account (3qX21hU5)
Well then get reading and start coding. I highly doubt they would have just gave you this problem and told you that if you don't pass it you will not graduate without teaching you how to do it. I mean this isn't exactly beginner "Hello World" stuff.

Not trying to be rude or anything.

Try and code it yourself for a hour then come back here with what you have. We won't care if it is no where near as close just as long as you try. After we see the code we can tell you what you will need to study, what you need to learn, and what you did wrong. We will even probably provide you some links to resources you can use for each subject you are struggling on.

So get coding we won't do it for you, its not our ass that is on the line.

Interesting, I had no idea there was a country called Georgia.

I think it was part of the soviet union or something. At least that is what I have heard about it before.
Last edited on
Alright Zereo I'll do my best
I appreciate the fact that you're even replying to me but I don't really have time to read any documentation, not trying to come off as rude, and I know this is a common excuse, but our teacher is extremely bad at explaining things, the evidence is that 18 kids out of 20 failed his "pseudo exam"
It's really hard to comprehend what he's saying or what he has written, I'll try to find a solution and post here.
What is wrong with this code?
1
2
3
4
5
6
7
8
9
  #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {
    int random_integer = rand();
    cout << random_integer << endl;
	system("pause");
    }


I'm getting same number again and again, isn't it suppose to be random?
closed account (3qX21hU5)
Try srand (time(NULL));\

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <time.h>      // You need this for the time() function
#include <iostream>

using namespace std;

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

    for (int i = 0; i != 10; ++i)
    {
        int randomInt = rand();
        cout << randomInt << endl;
    }

    return 0;
}



What it does is passes a seed to srand() from what is return from time(). This will give you semi random numbers.

srand() info can be found here - http://www.cplusplus.com/reference/cstdlib/srand/

time() info can be found here - http://www.cplusplus.com/reference/ctime/time/

rand() info can be found here - http://www.cplusplus.com/reference/cstdlib/rand/

I know you say you don't have time to read documentation but you are going to have to. So get ready to pull a all nighter.
Last edited on
Thanks Zereo, I've been googling and reading little by little of what I can
I figured out the main problem, I think that's a good thing only problem I have now is syntax, so basically here's the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    #include <iostream>
    #include <time.h>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    srand((unsigned)time(0));
    int random_integer;
    int lowest=-10, highest=10;
    int range=(highest-lowest)+1;
    for(int index=0; index<20; index++){
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << endl;
    system("pause");
    }
    }

what if I want to line up random numbers horizontally without having to press enter to generate one, how would I do that?
Replace endl with ' '.
Move system("pause"); outside the for() loop.
Thank you catfish3, here's my code now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    #include <iostream>
    #include <time.h>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    srand((unsigned)time(0));
    int random_integer;
    int lowest=-10, highest=10;
    int range=(highest-lowest)+1;
    for(int index=0; index<20; index++){
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << ' ';
	}
	cout<<"=============== \n";
    system("pause");
	
    
    }

the "===========" is written horizontally along with random numbers how do I write it below the numbers?
Just add another newline character.
cout<<"\n=============== \n";
Thank you catfish3
As expected, I'm stuck again..
how do I sort randomly generated numbers from lowest to highest?
You store them somewhere, like an array (I'd personally prefer std::list), sort the array, then display the array's contents. You can use the function std::sort() from the algorithm header.
Well I tried this
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
    #include <iostream>
    #include <time.h>
    #include <cstdlib>  
    #include <vector>
    #include <algorithm>



    using namespace std;
    int main()
    {
		vector<int> v;
    srand((unsigned)time(0));
    int random_integer;
    int lowest=-10, highest=10;
    int range=(highest-lowest)+1;
    for(int index=0; index<20; index++){
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << ' ';
	}
	
	cout<<"\n=============== \n"; 
	 sort(v.begin(),v.end());
    cout << endl << v[v.size()-2] << endl;
    system("pause"); 
	}

It's not working and I'm getting error window.
closed account (3qX21hU5)
Sort each number you generate in a container, personally I prefer vector<int> over list unless you are going to be doing a lot of insertion in the middle and deletion in the middle. If you use a list you won't have random access to the numbers either.

Here is a example

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
29
30
31
32
33
34
35
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <vector>       // Needed for vector<>
#include <algorithm>    // Needed for sort()

using namespace std;

int main()
{
    vector<int> numbers;   // Our containers to store all the numbers
    srand(time(NULL));

    for (int i = 0; i != 10; ++i)
    {
        int randomInt = rand() % 100 + 1;  // Generates a number 1 - 100
        cout << randomInt << endl;         // Prints the number
        numbers.push_back(randomInt);      // This will put the number on the
                                           // back of the vector
    }
    cout << "\n\n\n";

    // This will sort the vector from smallest to largest.
    // numbers.begin() is a pointer to the first element in the vector
    // and numbers.end() is pointer the last element in the vector
    // Basically you are telling it to sort from the first to the last element
    sort(numbers.begin(), numbers.end());

    // Prints everything in the vector
    for (int i =0; i != numbers.size(); ++i)
        cout << numbers[i] << endl;


    return 0;
}

closed account (Dy7SLyTq)
well first of all v has nothing in it and second of all, what are your errors?
Pages: 123