C++ :: Unable To Create Random Array Of 100 Int Between 0 And 250 To Sort Them And Print
Nov 25, 2013
I seem to get an error after int main (void) saying 'a function definition isnt aloowed here before { token? And then also at the end of main saying 'expexted } at end of output?
My programme is trying to create a random array of 100 ints between 0 and 250, sort them and print them.
Here is my code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>
void bubbleSort(int *array,int length)//Bubble sort function {
int i,j;
for(i=0;i<10;i++)
I am trying to sort an array of random numbers by passing it to a method Sort(), sort the array, and then pass the entire array back to the calling program. Each time I run this though, the array doesn't seem to sort. I'm not sure if the problem lies with my sorting algorithm or if it has something to do with calling the function.
Code:
static void HighAverage(int[] a) { for (int i = 0; i < a.Length; i++) { if ((i + 1) % 10 == 0 && i != 0)
My C programming class wants us to write a program to read integers into an array, sort the numbers and print out the occurrence of each number. I've tried everything that I can think of but the core dump is still occurring.
void countValues ( FILE *inf, int list[], int size ); /* Function prototypes. */ void printFrequencies( const int list[], int size ); int main (void) { int status = EXIT_SUCCESS; /* defaulting status to success. */ FILE *inf = fopen( "numbers.txt", "r" ); /* input data file */
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; int main () { ofstream fout("datain.txt",ios::out); int array[6][6];
I have been working with and trying to understand Boost for random number generating. I've come to understand that what I want is now in the C++11 standard, but I've stuck with Boost.
What I have done is create a simple main.cpp that generated random numbers successfully. From there I wanted to create a more optimal system by separating out the random code portion to a class that can be instantiated and called on demand. My problem now is that when I call the random routine of the class I just get one number no matter how many times it's called.
I know that rapid seeding is an issue so I put the seed function of the random portion in the class's constructer so that it is only called once on the creation of a class. I also tried making it a static member, but I haven't had success with that yet either. For now though, I would simply like to get successive random values upon calling the class's random function.
class header Code: #ifndef roomGen_hpp_ #define roomGen_hpp_ #include"boost/random.hpp" #include"main.hpp" class roomGen { private: boost::mt19937rngDimensions;
[code].....
The main.hpp file only contains a couple of struct definitions. In main I call the class with Code: randomRooms.push_back(randRooms.createRoom()); to get back a struct with random values and push it into a vector.
I'm unsure what I'm doing wrong here to only get back a single non random number from successive calls. I have tried moving the variable creation lines from roomGen::createRoom() to the private area of the class definition, but that causes me to get a slew of undefined errors so I had to settle on putting those declarations inside the function.
I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.
my text file is 0.0 0.0 0.0 1.0 1.0 0.0(but it read until here) 0.0 0.0(suppose to read the last point which is here)
For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)
*This is the code for my read file function*
Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.
Code:
polygon *plg_read(polygon *New_polygon, FILE *Coord) { int i; int numberofvertices=0; int count=0; char filename[50]; double xCoor[50], yCoor[50];
[Code]....
This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.
I'm VERY new in programming and I'm having trouble converting an integer to string. I need to create a function for a programme I'm working on for my school. My problem is that i am only allowed to use the libraries stdio.h, time.h and stdlib.h as well as printf, scanf, system, srand, time and rand. If I was allowed to use itoa or pointers it would be easier but i am not.
i'm trying to fill an array with random numbers and then sort them via bubblesort. it seems to work so far. the problem is, that i seem to get the same numbers for the same input. somehow the randomness isn't working.
Code:
#include <stdio.h> #include <stdlib.h> int main() { int a, b, c, d, e, f; }
I'm trying to sort random numbers in ascending order and I was wondering how I should go about that.
Here's what I currently have.
#include <iostream> #include <vector> #include <cstdlib> using namespace std;
[Code].....
I'm trying to put our algorithm between the ////'s. We're only allowed to use for loops also. What I currently have is the minimum number finder and the use of temp to find the values. However, it doesn't seem to be working.
1: Sort Alphabetically 2: Sort Grades Increasing Order (Student) 3: Sort Grades Increasing Order (Project) 4: End Program Enter choice:
Why my sort is not working. Also, I want to keep the same random numbers for the continuation of the program, I don't want new randomized values when I display the table.
Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.
Code: typedef unsigned long long UI64; typedef unsigned long long *PUI64; PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) { size_t mIndex[8][256] = {0}; /* index matrix */ PUI64 pDst, pSrc, pTmp; size_t i,j,m,n; UI64 u;
I'm just getting back into the swing of things after a long time of not programming. I'm trying to create a program which takes in command line arguments and prints the last one. My code is as follows:
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string x; vector<string> arguments;
[Code]...
And the error message I receive, a simple but frustrating one, is as follows:
Enter arguments, enter STOP to stop: Segmentation fault...
The program must print out 5 random numbers, from 1 to 45 and 100 different sequence.. Now I want each number of sequence to be different and not the same....
for example
1,2,3,4,5 6,7,8,9,10 .... ... ..
here is my code:
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int xRan1;
I have to write a c++ program to read html code and do bunch of stuff. One thing that i have to do is to count the no of attributes and sort them in descenting out and print out only first 10 frequently used attributes. I counted them using maps and sorted them using multimaps but now dnt knw how to print only 10 elements
for(std::map<string, int>::iterator it = Element.begin(); it != Element.end(); ++it) Elements.insert(pair<int, string>(it->second, it->first)); for(std::multimap<int, string>::reverse_iterator it = Elements.rbegin(); it != Elements.rend(); ++it) { cout << "Element: " << it->second << " : " << it->first << endl; }
This is how i did it . How to display only 10 elements and not all the elements.
I have an exercise that asks me to print numbers at random from the following set (using only a single statement):
2, 4, 6, 8, 10
Here's my statement:
Code: cout << (2 + rand() % 9) << " ";
which prints numbers at random between 2 and 10, now I can use the modulus operator in an if...else statement to print only even numbers but the exercise specifically requires using only one statement, can that be done using the conditional operator? and if not then how?
I am working on a small simple program my program fills up a air plane with customers there is 2 levels 1 and 2 and it will put that person in the spot depending on being picked.
So far i made a boolean airplane with 10 spots I can enter 1 person into the airplane with no problem but then after he sits down i get a infinite loop? I assume i maybe have to use the continue statement I feel like there is something simple that im missing i want to add more people. here is what i have
Im trying to figure out how to print a random number of asterisks on two separate lines at the same time. So every time you press a key it prints a different amount of random number of integers between1 and 10 until one of the lines reaches 70. I have the code to do one line but can't figure out how to do two at once.
#include <stdio.h> #include <time.h> #define MINR 1 #define MAXR 70 #define MINM 1 #define MAXM 10 int main (void)
How to create .cpp file (new class) that will handle all the random number generators (<random> library) that main could possibly access. When I looked for a way how to implement random generators the examples were always in the main function or in a class in the main.cpp (with no header file and the generator was not seeded as I imagine it should be = only once in a program).
Questions follows after this malfunction code.
main.cpp #include "randomizer.h" int main() { Randomizer randObject(0, 10, 125); //Set bounds and seed generator int mainVar = randObject.getRandInt(); //Store newly generated random number for future work
[Code] .....
1) So I think that I should somehow declare these generators and distribution in the header file. Is it like declaring int a;?
I tried writing std::default_random_engine defGen; into the header file which only silenced compiler's errors but defGen wasn't seeded.
I have seen some examples using auto a = randInt(defGen); (or maybe with the use of auto a = std::bind.... But what does auto represent? I can't use it in the header file.
2) Can I ensure that the randObject is seeded only once? If for examle I need randObject2 with different boundMax, can I still use the same defGen which was seeded the first time? That should ensure better "randomness" through the program, shouldn't it?
3) Is there better way (and easy too) to "interface" random number generators? The class in the end should provide "get" function to acces int, double, bool, signed/unsigned... or some other specialities like random prime numbers, etc.
If it is anyhow related I am using Code::Blocks IDE with GCC compiler (4.7.1)
I can't compile this code as I am at work and the computers are security protected, So i''l have to wait until i get home to test this, but I am pretty sure I am wrong. The problem is I have to create two vectors with 10 elements and input random numbers into it, then pick one of the elements of the second vector at random and append it to an element from the first vector at random. This has to be done 10 times and the I am assuming i have to print the 10 results. This is what I have:
#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { vector<int> random (10);