Register for your free account! | Forgot your password?

Go Back   elitepvpers Coders Den C/C++
You last visited: Today at 14:13

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Help] Array looping, sum

Discussion on [Help] Array looping, sum within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
[Help] Array looping, sum

Hi, well i got a problem, I am trying to fill my array with random numbers.. and i want to make it so that the filling ends as soon as the sum of filled elements in that array exceeds and integer (lets say int i=50; )

Code:
void fillArray() {
                         


        srand((unsigned)time(0));
       
        for(int i=0; i<50; i++){
                   v[i] = (rand()%50);          
        }
       
        int sum = 0;
        for(int a=0; a<50; a++){
          sum+=v[a];
              
        }

}
i got the filling and the sum, but when i try to make it in a do-while loop, and compare it with sum i get error, as sum is not defined..etc...
Hikarim is offline  
Old   #2
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
Code:
#include <iostream>
#include <iterator>
#include <vector>
#include <random>
#include <chrono>

template<typename Generator, typename OutputIterator>
OutputIterator generate_random_numbers(Generator gen, OutputIterator result, int sum_limit, int min, int max)
{
	int sum = 0;
	std::uniform_int_distribution<int> dist(min, max);
	while(sum < sum_limit)
	{
		int rand_int = dist(gen);
		*result = rand_int;
		++result;
		sum += rand_int;
	}
	return result;
}

int main()
{
	std::vector<int> vec;
	std::default_random_engine re;
	re.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
	
	generate_random_numbers(re, std::back_inserter(vec), 50, 0, 10);

	for(int i : vec)
	{
		std::cout << i << std::endl;
	}
}
Schlüsselbein is offline  
Old   #3

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
Schlüsselbein, maybe he's coding in C, not in C++.
Delinquenz is offline  
Old   #4
 
.SkyneT.'s Avatar
 
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
Quote:
Originally Posted by Delinquenz View Post
Schlüsselbein, maybe he's coding in C, not in C++.
It's not hard to translate the essential functionality to C.
.SkyneT. is offline  
Reply


Similar Threads Similar Threads
[C#]Parallel Looping
07/02/2012 - .NET Languages - 11 Replies
I want a function that gives me looping parallelize. Example : While(a == 1) { Send("{ENTER}"); }
[MYSRO]Db Bot V0.8 is Not Looping
08/14/2010 - SRO Private Server - 3 Replies
my problem is bot is not looping as i want it stays near the storage i did the tool for the loop it started looping and stopped near the storage again. i think its proxlem about Storage's x,y. can some1 fix it? Thanks.
Looping patch
06/16/2010 - Grand Chase Philippines - 3 Replies
Help please. Paulit ulit pinapatch char_script and script ko.
How can i stop a looping effect?
08/10/2009 - CO2 Private Server - 6 Replies
Yeah erm, like using c# not the ini's
Bot looping problem
04/24/2007 - Silkroad Online - 1 Replies
I have a config set up for my farmer but every time it gets to the bandit archer place it return scrolls , then goes and repairs then goes back. Then it will get hit by like 2 monsters then start to return scroll again. And I unchecked the durability "go back to town" options and it still did it. What did I do wrong. And does anyone have a configuration that i could use for my lvl 32 pure str blader that works good in a loop?



All times are GMT +2. The time now is 14:14.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.