Help needed on a quick, easy-to-solve program

Hi everyone!

I have a simple problem in C++ that I really need help solving. I generally solve these things by myself but the problem is I was away from home for a week and I only noticed today that I need to do a program for tomorrow. As you can see I'm really in a hurry and I don't have enough time to go through the theory on how to do these types of programs. Actually, I'm pretty sure I can go through the theory, I just wouldn't have the time to program it corretly in such short timespan (I'm not a programming genious I'm afraid).

My problem is this. I need a program that loads a random amount of numbers (user defined, for example 10 - they can be decimal), in a defined range from 0 to 127, store them in an array and for each entered number (whole number) I need to find an ASCII code equivalent char (so for user input 77 I would get M as a result), and I need to use binary search alghorythm to search the field (I think I need to sort it first too). I have to use functions wherever I can and since I'm a beginner it would be great if you would use the most basic commands here.

There are some additional elements needed for this program, but right now all I need to see is the code for this part of the program. Until someone solves this I'm pretty sure I'll manage to go through the theory by then and understand how the program works.

I would really appreciate help on this one, or at least pointers on how to do this if you don't want to write the code for me.

brijach
I dont know how you want the binary search. You can easily cast numbers to chars like this cout << (char)77;
Well, I can't help you with the whole ASCII thing, but I do know a thing or two about the rand() First off, you need to include this in the preprocessor:
#include <cstdlib>
This file has the random function.
Then you must make the random var.
Try this:
1
2
3
4
5
6
7
8
9
int main()
{
     int randNumber = rand();
     for (int i = 0; i < 10; i++)
     {
          cout << i << ") " << randNumber << endl;;
          randNumber = rand();
     }
}


this will give you 10 seemingly random numbers.
To edit the range, we need to use the modulus operator (%):

 
int randNumber = (randomNumber % 6) + 1; // get a number between 1 and 6 


Any positive number divided by 6 will give a remainder between 0 and 5, and then you move the entire range up one, so instead of 0-5 you have 1-6.

The problem with this is that they are not conpleatly random numbers; the compiler reads from a list of numbers. A way to insure that you will allways get random numbers is to make it read from the place in the list based on what time it is. The following code 'seeds' the random number generator:

 
srand(static_cast<unsigned int>(time(0)));


Even I don't completely understand this, but it basically selects a place to read from based on what time it is, like I said.

So, a random number code should look something simular to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
     srand(static_cast<unsigned int>(time(0)));

     int randNumber = (randomNumber % 6) + 1;
     cout << randNumber;

     int i;
     cin >> i;
}


I hope this helps!
Respond if you need anything else

Ok I've started writing a program using some elements from your responses. Will let you know what I come up with. I started going through the theory and I picked up on some things really fast.

And I have an idea for how you might convert the integers to ASCII. You could make an array of the chars like this:

1
2
3
4
5
6
char ASCIITable [127]=
{
     (first char),
     (second char),
     (etc...),
);


So then the program would output the corrisponding char in the list in the slot of the random number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char ASCIITable [127]=
{
     (first char),
     (second char),
     (etc...),
);

int randNums [10]; // assume that you have allready defined these

char outputChars [10]; // this is where the selected chars will be put

for (int i = 0, i < 10; i++)
     outputChars[i] = ASCIITable[(randNums[i] /* you may need to add + 1 or - 1 here...*/)];
 

for (int i = 0; i < 10; i++)
     cout << outputChars[i];


I'm a beginner too, probably right around your level, so I may be mistaken. Anybody feel free to correct this.
Actually, it has to go something like this (sorry for my bad explaining):

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<iostream>

using namespace std;

int main(){
    int array_size;
    cout << "Enter array size: " ;
    cin >> array_size;
//array size has to be user defined, for example: 10 elements in the array
// which will be entered by the user, they can be decimal
    float array[array_size];
    
    for (int i=0; i<array_size; i++){
again:    cout << "Input " << i+1 << ". element of the array: " ;
          cin >> array[i];
          if (array[i]>=0 && array[i]<=127){ cout; }
          else{
             cout << "WRONG INPUT! Haz to be between 0 and 127!" << endl;
             goto again; }
        }
           
    for (int i=0; i<array_size; i++){
        cout << array[i] << " " ;
        }


    //this is the part where i need to go through the array using binary search 
//and then convert each number into it's ASCII equivalent. and i don't know how to do that. help :D
        
    
    
    system("pause");
    return 0;
    
}


After I combine this with binary search I just need to figure out how to implement this into functions, everything that can be made a function has to be a function. I haven't reached functions theory yet tho and I'm sketchy at best on binary search. And yes, I realize, this is second grade programming but I'm seriously having time issues on this one. Any help would be appreciated.
Last edited on
I can help with functions. A function is a chunk of code, as you know. We use it to make our code more readable and make it easier on us. You must define the function in the preprocesser. Now, there are two ways of making the code of the function. I will show you my way:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

int add();
int subtract(); // these define the functions i will be using

int main () // this is also technicly a function :p
{
    int option;
    char yOrN = 'y';

    while (yOrN == 'y')
    {
        cout << "Enter 1 for addition and 2 for subtraction:";
        cin >> option;

        if (option == 1)
            add(); // this exicutes the function, later you will learn about the ()

        if (option == 2)
            subtract();

        cout << "Again? (y/n):"; // notice the lack of goto. GOTO IS BAD! STAY AWAY!
        cin >> yOrN;
    }
}

int add() // this is where you put the body of the function, treat it like int main for the mostly
{
    int numOne;
    int numTwo;

    cout << "\nNumber 1:";
    cin >> numOne;

    cout << "\nNumber 2:";
    cin >> numTwo;

    int product = numOne + numTwo;

    cout << "\nThe product is: " << product << endl;
    return 0;
}

int subtract()
{
    int numOne;
    int numTwo;

    cout << "\nNumber 1:";
    cin >> numOne;

    cout << "\nNumber 2:";
    cin >> numTwo;

    int product = numOne - numTwo;

    cout << "\nThe product is: " << product << endl;
    return 0;
}


This is basic funtions, tell me if you need more help, also, check here: http://www.cplusplus.com/doc/tutorial/functions/

p.s.
I see that you used a goto statment. Don't use goto, it is bad programming and you end up with spagetti code.
Topic archived. No new replies allowed.