Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Someone help and explain a project in C++

Posted on 2004-08-28
8
Medium Priority
?
310 Views
Last Modified: 2010-04-01
Good morning how are you all doing? I know your are all busy and I was wondering if someone could help me out here. I need to create a array fo random numbers in C++. I am having a hard time understanding how to do this so anyone is willing to help and explain I will really appreciate it. Thank you
0
Comment
Question by:D_basham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 800 total points
ID: 11920863
Here is a sample from MSDN

Example
// crt_rand.c
/* This program seeds the random-number generator
 * with the time, then displays 10 random integers.
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main( void )
{
   int i;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

   /* Display 10 numbers. */
   for( i = 0;   i < 10;i++ )
      printf( "  %6d\n", rand() );
}
Sample Output
   19430
   28222
    9710
   12070
    7513
    9501
    1767
   26041
   11872
    4097
0
 

Author Comment

by:D_basham
ID: 11920969
I have  a question though,  I have seen time with 0 instead of NULL what is that all about. Also I see %6 I have seen %N before what is that all about too? Sorry I just really want to understand what I am doing?
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11921015
NULL is just a macro defined as:
#define NULL ((void *)0)

So basically NULL and 0 is the same.

>printf( "  %6d\n", rand() );
You should take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_printf.2c_.wprintf.asp and http://msdn.microsoft.com/library/en-us/vclib/html/_crt_format_specification_fields_.2d_.printf_and_wprintf_functions.asp

The '6' is the width
The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values

The 'd' specifies an integer data format

The \'n' means that a 'newline' character is added after each number.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:nonubik
ID: 11921106
For your array, you should just do

int arrRnd[10];
for( i = 0;   i < 10;i++ )
      arrRnd[i] = rand() ;
0
 

Author Comment

by:D_basham
ID: 11921430
Thanks for taking the time to help me. I have one more favior. Could you explain the merge sort a little to me. I understand the concept of it but having a tough time starting it. Thanks
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11921766
A merge sort normal is soved recursively. You have two functions, mergesort und merge.

mergesort divides a given array into 2 parts and calls mergesort again for the left part and the right part.

merge merges two already sorted arrays that are two contigous parts of one array.

so sort an array you have simply to call mergesort passing the array and the array size. By recursion the array is divided into parts till the array size is 1. The merge call of the last recursion then brings two single items in the right order, the merge of last-1 recursion merges pairs, and so on. The last nerge call of the initial call to mergesort finally returns the sorted array.

Have a look to that question if you want to see code:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21098555.html

Regards, Alex

BTW, you should give all points to nonubik as he/she has suffuciently answered your initial question and either open a new question or increase the points of this question before closing it.




 

Have a look
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11922910
@Alex: thanx for support ;)
0
 

Author Comment

by:D_basham
ID: 11927310
Thank you so much nonbuik for all your help. My text book wasn't helping me enough with on how to do this project. Thanks for being a help.

itsmeandnobodyelse, thank you for the direction I ended up finding an example in one of my begining books that helped me too on how to do tha merge sort. That was a tricky one if you haven't done it before. Very, very efficent just a little more complex then a simple bubble sort. Thanks again both you you:)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question