Member Avatar for Member #897610

i make this progrm . first i generate this of 100 number .
then user cheaked the user how many time number is repeated in this series.
but i want random series of 100 number .
please check it & how to create a random series in this program.

#include<iostream.h>
#include<conio.h>
void generateData(int num []);
void printArray(int num[]);
int searchArray(int num[],int y);
int main()
{
int num[100],y;
generateData(num );
printArray(num);
searchArray(num,y)  ;
return -1;
}

void generateData(int num [100])
{


	for(int count=0;count<100;count++)
	{
		num[count]=count;
	  cout<<num[count]<<" ";

	}
	getch();clrscr();


}
void printArray(int num[100])
{
	 int count=0;
	for(int row=1;row<=10;row++)
	{
		for(int col=1;col<=row;col++)
		{
			for(;count<100 && count<10*row ;count++)
			{
			 cout<<num[count]<<"   ";
			}
		 }cout<<"\n";
	  }
}
int searchArray(int num[100],int y)
{
	 cout<<"enter the value THAT  u want fine taht how many it repeat that : ";
	 cin>>y;
	 cout<<endl;
	 int counter=0;
	for(int count=0;count<100;count++)
	{

	  if(y==num[count])
	  {
		counter++;
	  }

	}
	if(counter>0)
	{
		cout<<endl<<endl<<"the enter no is :"<<counter<<" times in table";
	}
	else
	{
	return -1;
	}
}

Dani AI

Generated

Quick, practical follow-up for (and thanks to for pointing to rand): prefer the C++11 <random> facility and a frequency map to count repeats. <random> separates engine (the generator) from distribution (the desired range), so it avoids many of rand()/srand() pitfalls and gives better control over reproducibility and distribution shape. (cppreference.com)

Problems in the posted code to fix before adding randomness: generateData writes sequential values, main calls searchArray(num,y) with y uninitialized, printArray’s nested loops produce the wrong layout, and non-standard headers/console calls (iostream.h, conio.h, getch, clrscr) are obsolete. main should return 0. Reading the search value in main and passing it into a well-named function (that returns the count) will make the logic clearer and safer.

A compact, modern approach (C++11+) — generate 100 numbers, print them, then count repeats with an unordered_map:

#include <iostream>
#include <vector>
#include <random>
#include <unordered_map>

int main() {
    std::vector<int> a(100);
    std::random_device rd;
    std::mt19937 gen(rd());               // or use fixed seed for reproducible runs
    std::uniform_int_distribution<int> d(0, 99);

    for (auto &x : a) x = d(gen);

    for (size_t i = 0; i < a.size(); ++i)
        std::cout << a[i] << ((i % 10 == 9) ? '\n' : ' ');

    std::unordered_map<int,int> freq;
    for (int v : a) ++freq[v];

    for (auto &p : freq)
        if (p.second > 1) std::cout << p.first << " -> " << p.second << " times\n";
    return 0;
}

This uses a robust engine and a uniform integer distribution, and counts frequencies in average constant time with std::unordered_map. (cppreference.com)

Notes: seed with a fixed integer (e.g., std::mt19937 gen(12345);) for reproducible tests. If the goal is 0..99 each exactly once, create an iota-filled vector and std::shuffle it instead of allowing repeats.

If you want random numbers, use srand and rand from cstdlib, and time from ctime:



This fills up an array of 5 integers with values ranging from 0 to 9, inclusive:

int a[5];
srand(time(0));
for(int i = 0; i < 5; i++)
{
    a[i] = rand() % 10;
}
// a[] now has random numbers in it.
Member Avatar for Member #897610

thnk u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.