Why am I stuck in a while loop?[c++] - c++

I'm stuck in a loop in here. I have a matrix filled with zeroes and I want to add k edges to a graph. I have no errors showed in Visual Studio. While it goes to this line of code:
int z = 0;
while (z!=k);
{
int a = std::rand() % n;
int b = std::rand() % n;
if ((v[a][b] != 1) && (a != b))
{
v[a][b] = 1;
v[b][a] = 1;
z++;
}
}
program gets stuck in infinite loop;
Here is full code:
std::vector<std::vector<int>> random_gnk(int n, int k)
{
srand(time(NULL));
int temp = 0;
for (int i = 0; i < n; i++)
{
temp = temp + i;
}
std::vector<std::vector<int>> v;
if (k > temp || k<0)
{
std::cout << "Blad. Podano zla liczbe krawedzi." << std::endl;
return v;
}
for (int i = 0; i < n; i++)
{
std::vector<int>row;
for (int j = 0; j < n; j++)
{
row.push_back(0);
}
v.push_back(row);
}
int z = 0;
while (z!=k);
{
int a = std::rand() % n;
int b = std::rand() % n;
if ((v[a][b] != 1) && (a != b))
{
v[a][b] = 1;
v[b][a] = 1;
z++;
}
}
return v;
}
void GNK()
{
int n, k;
std::cout << "Podaj wielkosc n grafu: " << std::endl;
std::cin >> n;
std::cout << "Podaj liczbe k krawedzi grafu: " << std::endl;
std::cin >> k;
print_matrix(random_gnk(n, k));
return;
}

Put your code through clang-format:
int z = 0;
while (z != k)
;
{
This should be clear: remove the semicolon!
There's an online formatter here. If you like it, get it set up with your IDE or code editor. It will solve problems like this and make your code more beautiful.

Related

Why am I getting C++ Runtime Error in Google Kickstart Round C 2020 problem 4 (Candies)

This is my first kickstart problem attempt (Round C 2020 problem 4 - Candies) and although my attempted solution works on vscode and passes the sample cases on the kickstart platform, it is giving me "runtime error" for test set 1. Any ideas why this is happening? I've tried changing the value of N (initially with a larger value it was giving me TLE) and "ints" to "long longs" but nothing seems to be working. I wrote my attempt based on the official analysis so I would have thought it would pass, although this is the first time I try using segment trees so maybe I'm missing something obvious with the implementation. Any help would be much appreciated.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 101;
int n;
vector<ll> t(2*N);
vector<ll> t1(2*N);
void build() {
for (int i = n - 1; i > 0; --i) t[i] = t[2*i] + t[2*i+1];
}
void build2() {
for (int i = n - 1; i > 0; --i) t1[i] = t1[2*i] + t1[2*i+1];
}
int sum(int a, int b) {
a += n; b += n;
int s = 0;
while (a <= b) {
if (a%2 == 1) s += t[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
if (b%2 == 0) s += t[b--];
a /= 2; b /= 2;
}
return s;
}
int sum2(int a, int b) {
a += n; b += n;
int s = 0;
while (a <= b) {
if (a%2 == 1) s += t1[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
if (b%2 == 0) s += t1[b--];
a /= 2; b /= 2;
}
return s;
}
void replace(int k, int x) {
k += n;
t[k] = x;
for (k /= 2; k >= 1; k /= 2) {
t[k] = t[2*k]+t[2*k+1];
}
}
void replace2(int k, int x) {
k += n;
t1[k] = x;
for (k /= 2; k >= 1; k /= 2) {
t1[k] = t1[2*k]+t1[2*k+1];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll T, q;
cin >> T;
for(int i = 0; i < T; i++){
cin >> n >> q;
for(int i = 0; i<n; i++){
int temp, temp2;
cin >> temp;
temp2 = temp;
temp = pow((-1),(i))*temp;
temp2 = pow((-1),(i))*temp2*(i+1);
t[i+n] = temp;
t1[i+n] = temp2;
}
build();
build2();
ll ans = 0;
char c;
ll l,r;
for(int i = 0; i<q; i++){
cin >> c >> l >> r;
l--;
r--;
if(c=='Q'){
if(l%2==1){
ans-=sum2(l,r)-((l)*sum(l,r));
}
else{
ans+=sum2(l,r)-((l)*sum(l,r));
}
}
else{
if(l%2==1){
replace(l,-(r+1));
replace2(l,-((r+1)*(l+1)));
}
else{
replace(l,(r+1));
replace2(l,((r+1)*(l+1)));
}
}
}
cout << "Case #" << i+1 << ": " << ans << endl;
}
}

is there other way to fill in array in C++ without vector

So I have array A and B, two of them contain random numbers and I need to write in the C array initially even numbers of A and B and then odd. I have made this wtih vector but I wonder if there is other way to do it like in Javascript there are methods like .unshift(), .push() etc
#include<iostream>
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
vector<int>C;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 == 0)
{
C.push_back(B[i]);
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 != 0)
{
C.push_back(B[i]);
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
I would suggest interleaving A and B initially:
for (int i = 0; i < n; i += 1)
{
C.push_back(A[i]);
C.push_back(B[i]);
}
And then partitioning C into even and odd elements:
std::stable_partition(C.begin(), C.end(), [](int i) { return i % 2 == 0; });
vector::push_back is the simplest way to have a collection that grows as you add things to the end.
Since you have fixed size for A and B, you could make them primitive arrays instead, which is what you have done. But for C you don't know how long it will be, so a collection that has a changeable size is appropriate.
You can use std::array, if you know the size you need in compile time. You can then add using an iterator.
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
std::array<int, n+n>C; // <-- here
auto C_it = C.begin(); // <-- here
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
*C_it++ = A[i]; // <-- here
}
if (B[i] % 2 == 0)
{
*C_it++ = B[i];
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
*C_it++ = A[i];
}
if (B[i] % 2 != 0)
{
*C_it++ = B[i];
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
Alternatively if you want to be more safe you can hold the next unwritten index and access elements with C.at(last++) = A[i], which checks for out-of-bounds and throws an exception instead of UB.
well you don't to change much.
first of declare C array as int C[n+n]; and declare a variable for incrementing through c array as int j=0;
and in if statements of loops do this C[j]=A[i]; j++; for first if and C[j]=B[i]; j++; for the second if statements
int main() {
const int n = 4;
int A[n];
int B[n];
int C[n+n];
int j=0;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C[j]=A[i];
j++;
}
if(B[i]%2==0){
C[j]=B[i];
j++;
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C[j]=A[i];
j++;
}
if (B[i] % 2 != 0)
{
C[j]=B[i];
j++;
}
}
j=0;
cout << endl;
for (int i = 0; i < C[].lenght(); i++)
cout << C[i] << " ";
}

all possible combinations to divide pack of candies

I have problem to solve and I'm stuck, I don't know how to start.
Suppose I have R childrens and S candies. I want to divide candies between childrens. Each child can get 0, 1, 2, 3 or 4 candies. How to find all the possibilities of such a division?
#include <iostream>
using namespace std;
void solve(int r, int s) {
if (s == 0)
{
cout << "no more candies" << endl;
return;
}
if (r == 0)
{
cout << "last child" << endl;
return;
}
for (int j = 0; j < 4 && j <= s; ++j)
{
cout << "r: " << r << " j: " << j << endl;
solve(r-1, s - j);
}
}
int main () {
int r, s;
cin >> r >> s;
solve(r, s);
return 0;
}
For now I have sth like this, I see in output that I have solutions here, but I don't know how to grab and store all possibilities into for example vector.
Just store counts and save variants at the last recursion level
vector<int> counts;
vector<vector<int>> sol;
void solve(int r, int s) {
if (s == 0)
{
sol.push_back(counts);
return;
}
if (r == 0)
{
return;
}
for (int j = 0; j <= 4 && j <= s; ++j)
{
counts[r - 1] += j;
solve(r - 1, s - j);
counts[r - 1] -= j;
}
}
int main() {
int r, s;
r = 3;
s = 5;
for (int j = 0; j < r; ++j)
counts.push_back(0);
solve(r, s);
for (int i = 0; i < sol.size(); i++) {
for (int j = 0; j < sol[i].size(); j++) {
cout << sol[i][j] << ' ';
}
cout << endl;
}
return 0;
}

HackerRank says ~no response on stdout~. C++

I wrote this solution for the absolute permutation problem on HackerRank. It works fine on dev-C++ but doesn't work on Hackerrank. I've found that the code produces output when I remove the abs_perm(). What's the problem here?
#include <iostream>
using namespace std;
int arr[100000];
int check(int n, int k)
{
if ( (2*k == n) || (k == 0) || (n - 4*k == 0) )
return 1;
else if (k < n/2)
return check(n - 4*k, k);
else
return 0;
}
void swap(int &a, int &b)
{
int c = b;
b = a;
a = c;
}
void ini(int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = i+1;
}
}
void abs_perm(int n, int k)
{
for (int i = 0; i < k; i++)
{
swap(arr[i], arr[k+i]);
}
if (2*k == n)
return;
for (int i = n - 1; i > n - k - 1; i--)
{
swap(arr[i], arr[i-k]);
}
if (n - 4*k == 0)
return;
abs_perm(n - 4*k, k);
}
int main()
{
int T;
cin >> T;
int N[T], K[T];
for (int i = 0; i < T; i++)
{
cin >> N[i] >> K[i];
}
for (int i = 0; i < T; i++)
{
cout << N[i] << " " << K[i] << "\n";
}
for (int i = 0; i < T; i++)
{
if ( !check(N[i], K[i]) )
cout << "-1\n";
else
{
ini(N[i]);
abs_perm(N[i], K[i]);
for (int j = 0; j < N[i]; j++)
{
cout << arr[j] << " ";
}
cout << "\n";
}
}
return 0;
}
Array is a structure to use when you know at compile time the dimension of your structure. What you wrote at the begin in abs_perm() is not correct for standard compilers (in fact you don't know the dimension of your array). You can use a std::vector or a std::list which allocate memory dynamically or (bad solution) you can allocate an array with dimension that certainly contains all elements you will put inside.

How do I get this random number generator out of an infinite loop?

I have an assignment for school where I need to create a lottery program. It is supposed to allow the user to input six numbers and then generate six random numbers for comparison. I got the inputs working, but I have encountered a problem where the random number generator (located in the while loop) is stuck in an infinite loop, and I have absolutely no idea what is causing it since I have never had an infinite loop in any previous programs. If someone could please look through the code and possibly establish what is wrong, I would greatly appreciate it.
#include<iostream>
#include<time.h>
using namespace std;
void randomizeSeed();
int randomRange(int min, int max);
int getInteger();
int main()
{
randomizeSeed();
const int minNumber = 1;
const int maxNumber = 49;
const int Size = 6;
int luckyNumbers[6] = {};
int randomNumber = randomRange(minNumber, maxNumber);
int winningNumbers[6] = {};
cout << "Enter six numbers between 1 and 49...\n";
{
for (int i = 0; i < Size; i++)
{
luckyNumbers[i] = getInteger();
}
for (int i = 0; i < Size; i++)
{
for (int i = 0; i < Size - 1; i++)
{
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
}
}
cout << "Lucky Numbers: ";
for (int i = 0; i < Size; i++)
{
cout << luckyNumbers[i] << " ";
}
cout << "\n";
cout << "Press any button to see the Winning Numbers.\n";
system("pause");
bool exist = true;
while (exist == true)
{
int count = 0;
cout << "Winning Numbers: ";
for (int j = 0; j < 6; j++)
{
winningNumbers[j] = randomRange(1, 49);
cout << winningNumbers[j] << " ";
system("pause");
}
}
}
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
int getInteger()
{
int value = 0;
while (!(cin >> value) || (value >= 50) || (value <= 0))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
for (int i = 0; i < Size; i++)
for (int i = 0; i < Size - 1; i++)
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
You have two loops and they both use i. You probably mean to use the second loop with another variable name, for example:
for (int i = 0; i < Size; i++)
{
for (int k = 0; k < Size - 1; k++)
{
if (luckyNumbers[i] > luckyNumbers[k + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[k + 1];
luckyNumbers[k + 1] = temp;
}
}
}
If you set your compiler warning level to 4 then compiler should warn you about these errors. Try to resolve all compiler warnings.