Thread: converted code

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    converted code

    Hello im new to the programing world im still in school and just finished a python class and starting tomorrow will be in a c++ class so to just mess around before class ive been trying to convert one of my programs from python to c++

    here is the python:
    Code:
    import random as R
    def array(size, initial=0):                                                     
        return [initial for i in range(size)]
    
    
    def randint():
        count = 0
        flag = 0
        rannum = array(100, 0)                                                     # Build an array with 1000 spaces
        while count != 100:                                                       # Loop and fill the array with 1-52 in random order
            rannum[count] = R.randint(1, 500)
            count += 1 
        print(rannum)
        print('count', count)
        
        while flag == 0:
            flag = 1
            k = 0
            while k <= (count - 2):
                if rannum[k] < rannum[k + 1]:
                    temp = rannum[k]
                    rannum[k] = rannum[k + 1]
                    rannum[k + 1] = temp
                    flag = 0
                k += 1
        print('Sorted List......')
        k = 0
        while k <= (count - 1):
            print (rannum[k])
            k += 1
    
    randint()


    Here is the c++ ive been working on:
    Code:
    //---------------------------------------------------------------------------
    #include <iostream>
    #include <tchar.h>
    #include <time.h>
    //---------------------------------------------------------------------------
    
    // School.cpp : Defines the entry point for the console application.
    //
    
    
    
    using namespace std;
    int main()
    {
    	int rannum[100];
    	int count;
    	int k;
    	int temp;
    	int lowest=1, highest=100;
    	int range=(highest-lowest)+1;
    	int flag;
    	int blank;
    	srand((unsigned)time(0));
    	count;
    	while (count != 100) {
    		rannum[count] = lowest+int(range*rand()/(RAND_MAX + 1.0));
    		count++;
    		//cout << rannum;
    		//cout << " count " << count << endl;
    		// cin >> blank;
    	}
    	while (flag == 0)
    	{
    		flag = 1;
    		k = 0;
    		while (k <= (count - 2))
    		{
    			if (rannum[k] < rannum[k + 1]) {
    				temp = rannum[k];
    				rannum[k] = rannum[k + 1];
    				rannum[k + 1] = temp;
    				flag = 0;
    				k++;
    			}
    		}
    		cout << "Sorted List";
    		k = 0;
    		while (k <= (count - 1))
    			{
    				cout << rannum[k];
    				k++;
                }
    
    	}
    	cout << "pause";
    	cin >> blank;
    	return 0;
    }

    can anyone give me some pointers

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by adamk8875 View Post
    can anyone give me some pointers
    What's the problem?
    Devoted my life to programming...

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly you have some uninitialized variables.

    Secondly your bubble-sort seems to be an infinite loop. You only increment the index if one item is smaller than another.

    Lastly you may want to move output out of the sorting code.

    BTW, standard library and existing language features can do a lot for you both in C++ and Python. E.g in Python your code could look like this:

    Code:
    import random as R
    
    def randint():
        rannum = [R.randint(1, 500) for i in xrange(100)]
        print rannum
        print('count', len(rannum))    
        rannum.sort(lambda a, b: cmp(b, a))
        print('Sorted List......')
        for item in rannum:
            print item
    
    randint()
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    lol wow that would have been nice to know when i was taking my python class the teacher was kinda off his rocker it seemed to me but anyway the python code i got from my college book it was in sudo so i did my best to put it to work one thing i have noticed with the c++ code is it generates the random numbers but im not sure if it is putting them in the right place cuz when it runs it print the memory address and it never changes

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    So when i was doing python it had a built in help file almost for the language features and modules where you could find different modules to use and how to use them is there anything like that for c++ or what would you guys recommend?

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    I haven't really done anything like this before, decided to have a go at it:

    Code:
    #include <algorithm>
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    
    int main()
    {
        int count = 0;
        bool flag = false;
        std::vector<int> rannum(100, 0); // Build an array with 1000 spaces
    
        while (count != 100) // Loop and fill the array with 1-52 in random order
        {
            rannum[count] = 1 + rand() % 500;
            count += 1;
        }
    
        for (int k = 0; k != count - 1; ++k)
            std::cout << rannum[k] << std::endl;
    
        std::cout << "count " << count << std::endl;
    
        while (!flag)
        {
            flag = true;
            
            for (int k = 0; k != count - 2; ++k)
            {
                if (rannum[k] < rannum[k + 1])
                {
                    std::swap(rannum[k], rannum[k + 1]);
                    flag = false;
                }
            }
        }
    
        std::cout << "Sorted List......" << std::endl;
        for (int k = 0; k != count - 1; ++k)
            std::cout << rannum[k] << std::endl;
    }
    If you want different random numbers each time it goes through, add "#include <ctime>" to the list of includes, and add "std::srand(std::time(0));" to the top of main().

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by adamk8875 View Post
    So when i was doing python it had a built in help file almost for the language features and modules where you could find different modules to use and how to use them is there anything like that for c++ or what would you guys recommend?
    C++ Reference [C++ Reference] or MSDN Library (google it!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM