Infinite loop (do - while) - infinite-loop

for(int m=0;m<=3;m++){
for(int n=0;n<=3;n++){
if(n>0){
int c =n,t=1;
do{
t = up_key_no0(&puzz[c][m]);
c--;
}while(t==1||c>=0);
}
}
}
int up_key_no0(int *puzy){
int *puzx = puzy -4;
int down = *puzy;
int up = *puzx;
if(((down==up)||(up==0))&&down!=0){
*puzx += *puzy;
*puzy=0;
return 1;
}
else{
return 0;
}
}
Is The Following piece of code wrong? if Yes Then Reply. The Whole Code Cant Be Fit But puzz is a 2 dimensional array of 4X4

Your do-while loop can go out of the range of the table to the negative indices when the t is 1 and the c is 0. So maybe you should change the condition to (t == 1 && c >= 0) (and instead of or).

I don't know that language is this, but case it's like Java, a "for" should be like so:
for (var i=0;i<=3;i++) {
}
Your while maybe wrong. This "==" on the while should be "=".
while(t=1||c>=0) {
}

Related

Climbing Stairs Problem ( Access of element in vector )

Below is a code for the problem of CLIMBING STAIRS https://leetcode.com/problems/climbing-stairs/
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n,0);
dp[0] = 1;
dp[1] = 2;
for(int i=2;i<n;i++){
dp[i] = dp[i-2]+dp[i-1];
}
return dp[n-1];
}
};
The code gives a RUNTIME ERROR of HEAP BUFFER OVERFLOW.
Looking at the code , if n==1 the code should return dp[n-1] i.e. dp[0] ,
but that does not seem to be the case.
I'm guessing the issue maybe related to access of elements in vector.
Can anyone please explain what could be the issue here ??
if n==1 the code should return dp[n-1] i.e. dp[0] , but that does not seem to be the case.
Yes.
But when n == 1, you call
dp[1] = 2;
so you access the second element when you have only one element.
And what about the case n <= 0 ?
So, maybe
int climbStairs(int n) {
if ( 0 >= 0 ) {
return ???;
} else if ( 1 == n ) {
return 1;
} else {
vector<int> dp(n,0);
dp[0] = 1;
dp[1] = 2;
for(int i=2;i<n;i++){
dp[i] = dp[i-2]+dp[i-1];
}
return dp[n-1];
}
}
The problem states the constraints are
1 <= n <= 45
You're going out of range when n is 1 (i.e. you only have dp[0] that scenario)

Why is erase function giving a runtime error?

vector<int> a;
a.push_back(0);
int n = a.size();
int cnt = 0;
for (auto itr = a.begin(); itr != a.end(); itr++)
{
if(*itr == 0)
{
cnt++;
a.erase(itr);
}
}
The code is working on inserting numbers other than zero.
The line a.erase(itr) is giving a runtime error for some reason.
Please help.
with erase you modify the vector so the iterator become invalid, a solution modifying a little your code :
vector<int> a;
a.push_back(0);
int n=a.size();
int cnt=0;
auto itr=a.begin();
while (itr != a.end())
{
if(*itr == 0)
{
cnt++;
itr = a.erase(itr);
}
else
++itr;
}
Note the right type for n and count is size_type rather than int

Correct method of implementing a recursive function in C?

I am trying to implement a recursive function into a piece of code. I have tried multiple methods but I can't quite get it right. I was wondering if there was anything I might have overlooked or if I didn't do it correctly at all.
For reference, the question I am trying to answer is the Waterloo CCC 2013 J3
#include <stdio.h>
int year, counter = 0, tempNum;
int isDistinct (int num) {
tempNum = num;
int number[10] = { 0 };
while (tempNum > 0) {
if (number[tempNum % 10] == 1) {num++; isDistinct(num);}
else { number[tempNum % 10] = 1; tempNum /= 10; }
}
return num;
}
int main() {
scanf("%d", &year);
printf("%d", isDistinct(year+1));
}

is String or is Number function

I'm actually working on a personal "Excel" for school.
When the value of my cell is a number (int), I want to add it in my listNumber (QList int). When the value of my cell is a String, I want to add it my listString.
These two lists then allow me to sort.
The problem is here :
QString test = text(i, j);
test.toInt(&ok);
if (ok == true) {
listNumber.append(test.toInt());
qSort(listNumber.begin(), listNumber.end());
}
ERROR ASSERT failure in QList<T>::at: "index out of range" .
I think it's because it wants to "insert" a string in a list of integer.
Here my function "sort"
QList<QString> listString;
QList<int> listNumber;
bool ok;
QTableWidgetSelectionRange range = selectedRange();
for (int j = range.leftColumn(); j <= range.rightColumn(); ++j) {
for (int i = range.topRow(); i <= range.bottomRow(); ++i) {
QString test = text(i, j);
test.toInt(&ok);
if (ok == true) {
listNumber.append(test.toInt());
qSort(listNumber.begin(), listNumber.end());
}
}
}
if (listNumber.count() == 0) {
QMessageBox test;
test.setText("liste vide");
test.exec();
}
else {
int x = 0;
for (int j = range.leftColumn(); j <= range.rightColumn(); ++j) {
for (int i = range.topRow(); i <= range.bottomRow(); ++i) {
Spreadsheet::setFormula(i, j, QString::number(listNumber.at(x)));
x++;
}
}
}
Thank you a lot for your help.
First of all, qSort in Qt is deprecated and it is recommended not to use it:
QT_DEPRECATED_X("Use std::sort") inline void qSort(...
You can use std::sort instead:
#include <algorithm>
//...
std::sort(listNumber.begin(), listNumber.end(), std::less<int>());
//or simply:
std::sort(listNumber.begin(), listNumber.end()); // using default comparison (operator <)
(But also you can simply call deprecated qSort:)
qSort(listNumber);

Facing incorrect output while linking programs

I am having a trouble in calling the user defined functions in the main program, using unix. the program is executing only for the number generation in the main program. but when i call the predefined function . the output retrieved is incorrect. Can someone please correct me where i have done it wrong
My main program states as
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i , j;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
generate random numbers
}
printf("The list of Hundred random numbers are \n");
for (j = 1; j <= 10; j++ )
{
//to print the random numbers
}
sort1(array);
return 0;
}
//this is my user defined function: sort1.c
include <stdio.h>
int sort1(int a[])
{
int array[100], i, d, swap, e=10;
// algortithm
}
}
printf("Sorted list in ascending order:\n");
for ( i= 0 ; i< e ; i++ )
printf("%d\n", array[i]);
}
I get the output as
ten random numbers in [1,1000]
The list of Hundred random numbers are
--This gives correct output
Sorted list in ascending order:
1
-1442229816
0
-1444472964
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
array[i] = rand()%1000 + 1;
}
printf("The list of Hundred random numbers are \n");
for (i = 1; i <= 10; i++ )
{
printf("Element[%d] = %d\n", i, array[i] );
}
//Up to here it's ok but you have set the values in the positions 1-10 of the array so keep consistent with it
sort1(array);
printf("Sorted list in ascending order:\n");
for ( i= 1 ; i<= 10 ; i++ )
printf("%d\n", array[i]);
return 0;
}
void sort1(int a[])
{
int i,swap,sorted=0;
while(sorted==0){ //flag to know if array is sorted 0 means not sorted.
sorted=1; // we set the flag to sorted at start of sweep
for (i= 1 ; i<= (10-1); i++) //sweep through array
{
if (a[i] > a[i+1]) //check if sorted
{
swap = a[i];
a[i] = a[i+1];
a[i+1] = swap;
sorted=0; //if not sorted set flag to 0 and swap
}
}
}
}
Main problems in your code:
1) array[100] is not initialized in sort1 function.
2) I do not understand your sorting algorithm but in any case you are checking for values of a[0] which are not initialized so take care of the array positions you use each time and be consistent with it.
3) function prototype does not match

Resources