Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ program

Name: Anonymous 2010-03-10 20:06

Made a short program that takes numbers, puts them into an array and compares them with each other. If any of your numbers are the same it gets you to type the numbers in again. This code seems to work fine but I though it's quite a lot for just comparing numbers, is there an easier/shorter way of comparing multiple numbers like this?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int count, test, mine[7], machine[7], count1, count2;
srand(time(NULL));
test=0;
count1=1;
count2=0;

while (test<1) {
     labelA:
    for (count=1;count<7;count++) {
        cout<<"Enter Number "<<count<<endl;
        cin>>mine[count];
        }
        while (count1<6) {
            for (count2=2;count2<7;count2++) {
                if (mine[count1]==mine[count2]) {
                    goto  labelA;
                    }
                    }
                    count1++;
                    for (count2=2;count2<7;count2++) {
                        if (mine[count1]==mine[count2]) {
                            goto  labelA;
                            }
                            }
                            count1++;
                            for (count2=3;count2<7;count2++) {
                                if (mine[count1]==mine[count2]) {
                                    goto  labelA;
                                    }
                                    }
                                    count1++;
                                    for (count2=4;count2<7;count2++) {
                                        if (mine[count1]==mine[count2]) {
                                            goto  labelA;
                                            }
                                            }
                                            count1++;
                                            for (count2=2;count2<7;count2++) {
                                                if (mine[count1]==mine[count2]) {
                                                    goto  labelA;
                                                    }
                                                    }
                                                    count1++;
                                                    test++;
                                                    }
                                                    }
return 0;
}

Name: Anonymous 2010-03-10 20:10

/g/ quality troll

Name: Anonymous 2010-03-10 20:12

>>2

Yes, because someone hasn't been programming since they were conceived is obviously a troll.

Name: Anonymous 2010-03-10 20:28

Dear Sussman, that's horrible. Even more horrible without [code] tags.

Name: Anonymous 2010-03-10 20:49

>>1
Just a few things to point:
-If you're going to initialize the variable in the for loop anyway, you don't exactly need to initialize it before.
-Dijkstra hates you. You know that, don't you?
-Your code is not very human readable, and you could get rid of those nested loop.
-You'll just need two loops, three at most.
-For more information, please read your SICP

P.S: You might want to make a control variable to serve as flag, maybe. Or if you're into breaks and continues... you get the idea.

P.S²: I'm not giving you any code. One can just achieve Satori by itself.

Name: Anonymous 2010-03-10 21:36

I beg your pardon, GTFO!

Name: Anonymous 2010-03-10 23:20

For Sussman sake! You should have showed us gore OP. It would be less harmful.

Name: Anonymous 2010-03-10 23:57

What is this I don't even

Name: Anonymous 2010-03-11 1:10

>>8
What is this I don't even
Back to the imageboards, please

Name: Anonymous 2010-03-11 1:32

>>8
>>9
I dunno but it uses GOTO.
OP: Don't use goto. Nobody likes it. It's only useful in assembly and a handful of other circumstances. Yes there is an easier way to do what you're doing, obviously.

In fact, there's probably a motherfucking one-liner somewhere.

Name: Anonymous 2010-03-11 1:44

Your code is demented and I'm not yet sure I mean that in a bad way.

If you want something functionally straightforward just keep concatenating numbers as C++ strings into a master C++ string (not char array, one of those other lowercase "strings") separated by a space.   You can use string::find(const string& str) to make sure it equals -1  and is not found in that master string; otherwise, start over.  Two loops at worst.

Name: Anonymous 2010-03-11 6:30

Here's a Lisp solution:

(defun unique-numbers-p (list)
  (maplist #'(lambda (x)
               (destructuring-bind (first . rest) x                
                 (when (find first rest)
                   (return-from unique-numbers-p nil))))
           list)
  t)

(loop until (unique-numbers-p (read)))

Name: Anonymous 2010-03-11 6:37

#include <algorithm>
#include <ctime>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

int main(int argc, char* argv[]) {
    const int quantity = 7;
    vector<int> mine;
    int x = 0;
    int i = 0;
    cout << "Please enter " << quantity << " non-0 integers:" << std::endl;
    while (i < quantity && cin >> x) {
        if (std::find(mine.begin(), mine.end(), x) == mine.end()) {
            mine.push_back(x);
            i++;
        }
    }
    cout << "Thank you for your " << quantity << " integers. The sum: " << std::accumulate(mine.begin(), mine.end(), 0);
    getchar();
    return 0;
}

Name: Anonymous 2010-03-11 6:40

>>13
I could have added a mine.reserve(quantity);
I'm also aware that I did not indicate to the user that there should not be duplicates and it's probable that you'd want another loop to ensure that the user enters 7 numbers before doing any processing, I don't really care.

Name: 12 2010-03-11 6:55

And an even more general solution:

(defun unique-sequence-p (sequence)
  (= (length sequence)
     (length (remove-duplicates sequence))))

Name: Anonymous 2010-03-11 10:25

Please, don't do the kids' homework.
Kthxbye

Name: Anonymous 2010-03-11 10:27

Kthxbye
VALID LOLCODE

Name: Anonymous 2010-03-11 12:49

>>17
You, sir, have made my day better.

Name: Anonymous 2010-03-11 16:46

This is now a LOLCODE thread.

Name: Anonymous 2010-03-11 18:15

HAI

CAN HAS HAX MY ANUS?

KTHXBYE

Name: Anonymous 2010-03-11 18:43

>>19
Everyday is LOLCODE day.

Name: OP 2010-03-14 15:17

I wasn't looking for handouts or anything, I tried this before and it didn't work (obviously I typed something wrong) but this does the same as my original code did (plus it compares 5&6 too, if anyone spotted that).


#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int count, test, mine[7], machine[7], count1, count2;
    srand(time(NULL));

    do {
        test=0;
        for (count=1;count<7;count++) {
            cout<<"Enter Number "<<count<<endl;
            cin>>mine[count];
        }
        for (count1=1;count1<7;count1++) {
            for (count2=count1+1;count2<7;count2++) {
                if (mine[count1]==mine[count2]) {
                test++;
                }
            }
        }

    } while (test>0);
    return 0;
}


I'm not used to the text boards, I didn't realise it'd keep code::blocks' silly indentation. From what you've guys said it could be even shorter but that far surpasses my days worth of knowledge.

Name: Anonymous 2010-03-15 9:27

>>22

[quote]plus it compares 5&6 too, if anyone spotted that[/quote]


I would sooner try and find a piece of string in a tub fool of needles using only my face than try and find logic errors in your code.

Name: Anonymous 2010-03-15 9:28

>>23

>>22

plus it compares 5&6 too, if anyone spotted that

I would sooner try and find a piece of string in a tub full of needles using only my face than try and find logic errors in your code.


FIXED THAT FOR YOU

Name: Anonymous 2010-03-18 15:01

>>23

>>22 doesn't look bad, does it?

Name: Anonymous 2010-03-18 15:14

man qsort

Name: OP 2010-03-18 17:54

Here's the whole program, almost done I think (we haven't been fully told what it must do). I know that srand can generate duplicate numbers but I'm undecided on how I should go about rectifying this; whether I should do like I have for the user entered numbers and keep trying (not really lottery like :<)or have a set pool of numbers, randomize these then pull out the first 6 and use them as the machine's numbers.


#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int count, test, mine[7], machine[7], count1, count2, temp;
    srand(time(NULL));
    do {         //fills mine & machine arrays, ensuring no user entered numbers are the sames
        test=0; //resets test variable
        for (count=1;count<7;count++) { // runs 6 times
        cout<<"Enter Number "<<count<<endl; //prompts user to enter numbers
        cin>>mine[count];                  //assigns entered numbers to count array
        machine[count]=(rand()%49)+1;     //generates numbers for machine array
        }
        for (count1=1;count1<7;count1++) { //runs 6 times
            for (count2=count1+1;count2<7;count2++) { //runs 6 times
                if (mine[count1]==mine[count2]||mine[count1]==0||mine[count1]>49) { //runs if a number in mine array matches another, 0 or is higher than 49
                    test++;
                }
            }
        }
    }
    while (test>0); //contiues to run until the if statement isn't triggered in a loop
    for (count1=1;count1<7;count1++) { //runs 6 times
        for (count2=count1+1;count2<7;count2++) { //runs 6 times
            if (mine[count1]>mine[count2]) { //sorts mine array
               temp=mine[count1];
               mine[count1]=mine[count2];
               mine[count2]=temp;
            }
            if (machine[count1]>machine[count2]) { //sorts machine array
               temp=machine[count1];
               machine[count1]=machine[count2];
               machine[count2]=temp;
            }
        }
    }
    for (count=1;count<7;count++) { //runs 6 times
        cout<<mine[count]<<", "; //displays numbers from mine array
    }
    cout<<endl;
    for (count=1;count<7;count++) { //runs 6 times
        cout<<machine[count]<<", "; //displays numbers from machine array
    }
    cout<<endl;
    count=0;
    for (count1=1;count1<7;count1++) { //runs 6 times
        for (count2=1;count2<7;count2++) { //runs 6 times
            if (mine[count1]==machine[count2]) { //checks matches between entered and random generated numbers
                count++;
            }
        }
    }
    cout<<"You matched "<<count<<" Balls";
    return 0;
}

Name: Anonymous 2010-03-18 19:06

>>27
Any program that outputs the response "You matched balls" worries me.

int i = 0, j = 0;
int mine[] = { 0, 0, 0, 0, 0, 0 };
for(; i < 6; i++)
{
   cout<<"Enter Number "<<(i+1)<<": ";
   cin>>mine[i];
   if(mine[i] == 0 || mine[i] > 49)
   {
      cout<<"Duplicate in 'mine' numbers.  Start over ..."<<endl;
      i = -1;
      continue; // restarts this outer for() loop
   }
   if(i > 0)
   {
      for(j = 0; j < i; j++)
      {
         if(mine[i] == mine[j])
         {
            cout<<"Duplicate in 'mine' numbers.  Start over ..."<<endl;
            i = -1;
            break; // exits this inner for() loop
         }
      }
   }
}

Name: OP 2010-03-18 19:44

>>28

It's meant to be a lottery program and the balls thing was a joke by the tutor, he gave us a few lines of code to start off and I never got round to changing that (although I did have an if statement to display either ball or balls; whichever applied) or the "Enter Number 1" even though people don't like arrays starting at anything other than 0 but I didn't even think of doing i/count+1 there.

A couple of questions if you don't mind.
Is there a reason why you used int mine[] = { 0, 0, 0, 0, 0, 0 }; as opposed to int mine[6]; in this case? As far as I know since you aren't using the numbers before they are entered there does it really matter if you zero them?
i = -1; is the same as i--; right?

And thank you for showing me a better way of entering numbers, much less typing required for the user. I guess I will use your code for entering numbers and have all my arrays start at 0 then use a shuffled pool of numbers for the machine's numbers :)

Name: Anonymous 2010-03-18 20:22

>>29
Is there a reason why you used int mine[] = { 0, 0, 0, 0, 0, 0 }; as opposed to int mine[6];  in this case?
Just a personal design choice. +safety -speed probably.  You can safely disregard it here.

i = -1; is the same as i--; right?
No.  You might be thinking of i -= 1 which performs the same task as i-- and i = i - 1.  Based on your code, I was assuming you wanted the user to re-enter ALL their numbers if even one of them is duplicated. i-- would decrement the variable by one and have you re-enter the same number again on the loop. i = -1 would cause the loop to start inserting all the numbers in the array again. Depending on what your instructions were, or what your preference is, you could do either.

Also, I just thought of something that even I didn't see the first time: negative numbers.
if(mine[i] <= 0 || mine[i] > 49)

Name: Anonymous 2010-03-18 20:40

>>30
+safety -speed probably.
lol wat

Name: Anonymous 2010-03-18 20:53

>>31
If the values in the array are already defined (with defaults), there's no risk of garbage data regardless of when you access them.  I'm just assuming the "minus speed" part since it seems like I spend insignificant time to initialize the array while declaring it in the longer case.

Name: Anonymous 2010-03-18 20:55

>>32
Just use int mine[6] = {0}; - best of both worlds.

Name: OP 2010-03-18 21:08

>>30

Oh right! I didn't realise that the 'int' variable could be a negative number too, I thought that was reserved for another variable type like with non integers (eh..lol), thanks again.

>>33

ooh, pretty.

Name: Anonymous 2011-02-02 22:48

Name: Anonymous 2011-02-04 14:30


Don't change these.
Name: Email:
Entire Thread Thread List