2-D array of zeroes and ones - c++

I want to generate a 2-D array of zeroes and ones so that it is 25% zeroes and 75% ones.
I know that I will be using the rand()%2 function but how do i limit the zeroes to be only 25 percent of the array?

Create vector of size N with zeroes.
Set the first N*0.75 elements to one.
Randomize the vector.
Example code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iterator>
int main ()
{
std::srand ( unsigned ( std::time(0) ) );
const int N = 100;
const int zero_percent = 25;
const int one_percent = 100-zero_percent;
const int one_count = (N * one_percent)/100;
std::vector<int> v(N);
std::fill(v.begin(), v.begin()+one_count, 1);
std::random_shuffle (v.begin(), v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
return 0;
}
output
1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1
live example: http://ideone.com/CdlaMy

You could initialize every element of the array with the following:
int zeroOr1() { // Generates
static std::default_random_engine gen;
static std::uniform_int_distribution<int> dist(0,3);
return (dist(gen) % 4) >= 1;
}
Because dist(gen) % 4 produces one of [0, 1, 2, 3] about equally likely, (dist(gen) % 4) >= 1 will evaluate to true about 75% of the time and false about 25% of the time.
Note: this doesn't guarantee a perfect 25/75 distribution like the other answers, but it works for all array sizes. I'd need more information about the application to decide if this is good enough or not suitable.

If there are only 0s and 1s, one possible solution would be keeping all the coordinates in some kind of a "set" and randomly taking out then ( N/M /4 (n and m are the sizes of the array ) times ( to get 25% random coordinates) ) and mark as 1, then mark the rest as 0.

Related

Generate graph with vertices equal to elements of std::next_permutation, with edges between pairwise adjacent permutations

Consider the code snippet:
#include <vector>
#include <algorithm>
#include <stdio.h>
int main(){
int szvec = 4;
std::vector<int> vecint(szvec);
for (size_t i = 0, szi = szvec; i < szi; i++){
vecint[i] = i;
}
do{
for (size_t i = 0, szi = vecint.size(); i < szi; i++){
printf("%d\t", vecint[i]);
}
printf("\n");
} while (std::next_permutation(vecint.begin(), vecint.end()));
}
The output of this is the set of 4! different permutations.:
0 1 2 3 (Permutation 1)
0 1 3 2 (Permutation 2)
0 2 1 3
0 2 3 1
0 3 1 2
0 3 2 1
1 0 2 3
1 0 3 2
1 2 0 3
1 2 3 0
1 3 0 2
1 3 2 0
2 0 1 3
2 0 3 1
2 1 0 3 (Permutation 15)
2 1 3 0
2 3 0 1
2 3 1 0
3 0 1 2
3 0 2 1
3 1 0 2
3 1 2 0
3 2 0 1 (Permutation 23)
3 2 1 0 (Permutation 24)
I am interested in constructing an undirected graph with 24 nodes, (each node represents one permutation), with an edge connecting i and j if these permutations differ from each other in exactly one contiguous pair of elements. For instance, Permutation 1 and Permutation 2 will be connected since they differ in the pairwise exchange over contiguous indices. Permutation 1 and Permutation 15 will NOT be connected since even though they differ in pairwise exchange of elements, this exchange is not over contiguous indices. Permutation 23 and Permutation 24 will be connected.
The only way I can think of now of how to do this is to select each pair of permutations and evaluate explicitly whether they should be connected. Is there a more efficient way of doing this analytically? What I mean by analytical here is that given a particular node i, can we efficiently enumerate all other permutations it will be connected to? Note that szvec can be an arbitrary integer, not only 4.
ETA: With szvec = 3, we have six permutations:
0 1 2 (Permutation 1)
0 2 1 (Permutation 2)
1 0 2 (Permutation 3)
1 2 0 (Permutation 4)
2 0 1 (Permutation 5)
2 1 0 (Permutation 6)
and the graph looks so:
___________________
| |
5 - 6 - 4 - 3 - 1 - 2

Given a directed graph and two vertices ‘u’ and ‘v’ in it, count all the possible walks from ‘u’ to ‘v’ with exactly k edges on the walk

My code is failing for the following test case , please help
1
10
1 1 1 1 0 0 0 1 0 1
0 1 1 0 0 0 1 1 1 1
0 0 0 1 0 1 1 1 0 1
0 0 0 1 0 0 0 1 1 1
1 1 1 1 1 1 1 1 0 1
0 0 0 0 0 1 0 0 1 0
1 0 1 0 1 0 1 1 1 0
0 1 0 0 0 0 1 0 1 0
0 1 0 1 1 0 0 0 0 1
1 0 1 0 1 1 1 0 1 1
2 7 3
Its Correct output is:
11
And Your Code's output is:
6
Given a directed graph and two vertices ‘u’ and ‘v’ in it, count all the possible walks from ‘u’ to ‘v’ with exactly k edges on the walk.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
The first line of each test case is N which is number of vertices in input graph.
The second line of each test case contains N x N binary values that represent graph[N][N].
The third line of each test case contains u, v, k where u is starting position, v is destination and k is number of edges.
Output:
Print all possible walks from 'u' to 'v'.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 20
0 ≤ graph[][] ≤ 1
Example:
Input
1
4
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
0 3 2
Output
2
Explanation:
For example consider the following graph. Let source ‘u’ be vertex 0, destination ‘v’ be 3 and k be 2. The output should be 2 as there are two walk from 0 to 3 with exactly 2 edges. The walks are {0, 2, 3} and {0, 1, 3}
MY CODE
#include <iostream>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--)
{
int r,c;
cin>>r;
c=r;
int arr[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>arr[i][j];
}
}
int u,v,k;
cin>>u>>v>>k;
int dp[r][k+1];
for(int i=0;i<r;i++)
{
for(int j=0;j<k+1;j++)
{
dp[i][j]=0;
}
}
dp[u][0]=1;
for(int j=0;j<k+1;j++)
{
for(int i=0;i<r;i++)
{
if(dp[i][j]!=0)
{
for(int x=0;x<r;x++)
{
if(arr[i][x]==1)
{if(j+1<k+1)
dp[x][j+1]++;
}
}
}
}
}
cout<<dp[v][k]<<endl;
}
return 0;
}

C++ array sort is giving unexpected results

My code is giving unexpected results. This code is for sorting the elements in the array. Upon running it gives different answer. Can anyone please suggest where the problem might be?
void func(int *arr,int N){
sort(arr,arr+N);
for(int i=0;i<N;i++){
cout<<arr[i]<<" ";}
cout<<endl;
}
int main() {
int N;
int *arr=new int[N];
cin>>N;
for(int j=0;j<N;j++){
cin>>arr[j];
}
func(arr,N);
return 0;
}
Input:
84
1 0 1 2 1 1 0 0 1 2 1 2 1 2 1 0 0 1 1 2 2 0 0 2 2 2 1 1 1 2 0 0 0 2 0 1 1 1 1 0 0 0 2 2 1 2 2 2 0 2 1 1 2 2 0 2 2 1 1 0 0 2 0 2 2 1 0 1 2 0 0 0 0 2 0 2 2 0 2 1 0 0 2 2
Output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 10 807415840 807415840 807415840
807415840 807416096 807416096 807416352 807416352 824193056 824193056
824193312 824193312 824193312 824193568 824193568 824193568 840970272
840970272 840970272 840970272 840970272 840970272 840970272 840970272
840970528 840970528 840970784 840970784 840970784 840970784 840970784
You have an uninitialized variable in your code.
int N;
From the online cpp reference on Uninitialized variables:
It is possible to create a variable without a value. This is very dangerous, but it can give an efficiency boost in certain situations. To create a variable without an initial value, simply don’t include an initial value:
// This creates an uninitialized int
int N;
The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it.
So before allocating memory of the array based on the value of N, initialize it. In your case, read into it first.
cin>>N;
int *arr=new int[N];
It is also a good practice to check if cin has succeeded and if the value of N is within acceptable bounds before using it.
You are creating the array when N has an indeterminate value, so the result of your program is undefined.
Move this line;
cin>>N;
before this line:
int *arr=new int[N];

How to fix a bug in my homework solution in C++?

I need to write a program which reads the statistics of n League A football teams and prints the teams name which fall in League B.
A team falls in League B, if it has less than k points after having played m weeks where m is between 1 and 150. Each team gets three points for a win, one point for draw and zero points when lost.
Input Specification: In the first line, you will be given the number of teams 0 < n ≤ 500 and the points 0 < k ≤ 300 needed to stay in league A. Then in the following n lines, there will be the team name and its results. Semicolon indicates the end of input series.
Number 2 represents win, number one represents draw and number zero represents loss.
Output specification:
Sample Input I
4 19
Team_A 1 1 1 1 1 1 1 1 1 0 1 1 1 0 2 1 0 ;
Team_B 0 1 0 2 2 1 1 0 1 1 0 2 0 1 0 0 2 ;
Team_C 0 0 1 0 2 2 2 1 1 1 1 1 0 0 2 1 2 ;
Team_D 0 1 0 1 2 1 2 1 0 0 0 2 2 2 0 0 0 ;
Sample Output I
Team_A 16
Team_B 18
This is the code I came up with, but the output is wrong and I don't know why,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n,points,sum=0,i,value;
char name[15];
char p;
scanf("%d %d",&n,&points);
for(i=1;i<=n;i++)
{
scanf("%s",&name);
do
{
scanf("%c ",&p);
if(p!=';')
{
value=p-48;
sum=sum+value;
}
}while(p!=';');
if(sum<=points)
printf("%s %d",name,sum);
}
return 0;
}
You might look for problems by stuffing the program with output statements.
If you add after scanf("%c ",&p); an output statement to show the value of p, you will find that the first value for p is a space character, which spoils your calculation.
In the same way, if you trace the value of value, you will find that you forgot to initialize this variable to zero for each team.

int variable not resetting on c++?

So, my program is supposed to receive test inputs like:
3
1 0 1
0 1 1
1 0 1
5
1 1 1 0 0
1 1 0 1 1
1 0 1 0 1
0 1 0 1 0
0 1 1 1 1
3
1 0 0
0 1 0
0 0 1
2
1 1
1 1
0
where the single-valued lines (n) are the size of a NxN matrix located in the following n entries like shown above. If n = 0, the program stops. The output must be the biggest sum amongst the columns of the matrix. So I expect outputs like this:
3
4
1
2
After a lot of effort and wasted time, I managed to get the first output correctly, but I noticed the following ones sometimes summed up and suggested some variable was not being reset. Here's my code:
#include <iostream>
using namespace std;
int pop = 0;
int main() {
int n, i, j, k;
cin >> n;
while (n!=0) {
int alunos[n]={0};
pop = 0;
for (i=0;i<n;i++) {
int array[n]={0};
for (j=0;j<n;j++) {
cin >> array[j];
if (array[j]==1) alunos[j]++;
}
}
for (k=0;k<n;k++) {
if(alunos[k]>pop) pop = alunos[k];
}
cout << pop << endl;
cin >> n;
}
return 0;
}
Noticed that I'm outputting pop(the biggest sum) and resetting it to 0 everytime a new n is given. alunos[n] is an array with the sums of each column (also resetted on every while loop) and array[n] is just an auxiliary array for reading each line of input. My outputs with this are:
3
5
6
8
Thanks in advance!
You cannot use initializers with variable length arrays. Either switch to some sort of container:
std::vector<int> alunos(n);
or fill the array with zeros manually:
int alunos[n];
std::fill(alunos, alunos+n, 0);
Also, ignoring errors is unhealthy. Don't do it.