The fiboEncoding() function below is to read an integer then return the Fibonacci encoding.
When I test it in the main function, it always pushes itself into the most left part of the output. How can I solve this problem? What did I do wrong to cause this problem?
#include <iostream>
#include <vector>
using namespace std;
string fiboEncoding(int n) {
string word;
int fib[1000];
fib[0] = 1;
fib[1] = 2;
int i = 0;
for(i = 2; fib[i-1] <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
int r = i - 2;
int index = r;
vector<char> v(r+3);
while(n > 0) {
v[index] = '1';
n = n - fib[index];
index = index - 1;
while (index >= 0 && fib[index] > n) {
v[index] = '0';
index = index - 1;
}
}
v[r + 1] = '1';
for (int j = 0; j < v.size() - 1; j++) {
cout << v[j];
}
return word;
}
int main() {
int n;
string fibo;
cin >> n;
fibo = fiboEncoding(n);
cout << "code: " << fibo << endl;
}
Your function returns an empty string word. You forgot to copy the result into word string.
What you see in the console is the result of executing the following part not cout.
for (int j = 0; j < v.size() - 1; j++) {
cout << v[j];
}
To fix replace the above for loop by
for (int j = 0; j < v.size() - 1; j++) {
//cout << v[j];
word += v[j];
}
Related
I have this task:
A user inputs a number N and you have to output this pyramid:
0
101
21012
.......
N.21012.N
For N=5 it will be :
0
101
21012
3210123
432101234
54321012345
I managed to only get it working for N<10 with this code:
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n - i; j++)
cout << " ";
int dir = -1;
for (int k = i; k <= i; k += dir) {
cout << k;
if (k == 0)
dir = 1;
}
cout << endl;
}
For N=10 it will look like this :
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
9876543210123456789
10987654321012345678910
After the answers I settled on this :
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int n, spaces;
string number;
cin >> n;
if (n < 10)
spaces = n;
else
{
spaces = 9;
int pwr = 0, k = n;
while (k > 9)
{
pwr++;
k /= 10;
}
for (int i = 1; i < pwr; i++)
{
spaces += pow(10, i) * 9 * (i + 1);
}
spaces += (n - pow(10, pwr) + 1) * (pwr + 1);
}
// cout << spaces << endl;
for (int i = 0; i < n + 1; i++)
{
for (int j = i; j > -1; j--)
number += to_string(j);
int len = number.length() - 1;
for (int j = 0; j < spaces - len; j++)
cout << " ";
for (int j = 1; j <= i; j++)
number += to_string(j);
cout << number << endl;
number.clear();
}
cout << endl;
return 0;
}
int padding(int n) {
constexpr auto singleDigitNumbersCount = 9;
constexpr auto doubleDigitNumbersCount = 90; // from 10 to 99
if (n < 10) return n;
if (n < 100) return 2*n - singleDigitNumbersCount;
return 3*n - doubleDigitNumbersCount - 2*singleDigitNumbersCount;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
std::cout << std::string(padding(n) - padding(i), ' ');
for (int k = i; k >= 0; k--) {
cout << k;
}
for (int k = 1; k <= i; k++) {
cout << k;
}
cout << '\n';
}
return 0;
}
https://godbolt.org/z/EEaeWEvf4
I made this a bit ago Compiler Explorer
Not sure if that'd help 🤔
Here is the working code:
#include <string>
#include <iostream>
using namespace std;
#define MAX_SPACE 50
int main()
{
int n;
cin >> n;
string output = "";
for (int i = 0; i < n + 1; i++)
{
for (int k = i; k >= 0; k--) {
output += to_string(k);
}
for (int k = 1; k <= i; k++) {
output += to_string(k);
}
for (uint8_t i = 0, max = MAX_SPACE - output.length() / 2.00; i < max; i++) // Print max spaces minus the integer length divided by 2
{
cout << " ";
}
cout << output << endl; // Print number
output = "";
}
return 0;
}
so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1
I was trying to do a problem on HackerEarth, and I am getting Segmentation Faults for this for loop:
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
Even though I assigned values to arr_list[arr_index][index] in the loop right before (so I'm guessing the values are somehow not being saved, but I don't know how the values aren't being saved).
When I remove this for loop, I don't get any segfaults, and the cout information prints what's expected (the numbers I've inputted, with each digit twice for each cout inside the loop).
#include <iostream>
#include <string>
using namespace std;
void step(int arr_list[1000000][18], int cs, int N){
/**
int freq[100000] = {0};
for (int i = 0; i < N; i++){
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
freq[cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2]] += 1;
} else{
freq[cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4]] += 1;
}
}
for (int i = 1; i < 100000; i++){
freq[i] += freq[i-1];
}
int new_arr_list[1000000][18];
for (int i = N-1; i >= 0; i--){
int pos;
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
pos = cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2];
} else{
pos = cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4];
}
for (int index = 0; index < 18; index++){
new_arr_list[freq[pos] - 1][index] = arr_list[i][index];
}
freq[pos] --;
}
for (int i = 0; i < N; i++){
for (int index = 0; index < 18; index++){
arr_list[i][index] = new_arr_list[i][index];
}
}
**/
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
int arr_index = 0;
cin >> T;
int arr_list[1000000][18];
int max_len = 0;
for (int testcase = 0; testcase < T; testcase ++){
string a;
cin >> a;
int a_len = a.length();
if (a_len > max_len){
max_len = a_len;
}
int arr_entry[18];
for (int i = 0; i < a_len ; i++){
arr_entry[18 - a_len + i] = a[i] - 48;
}
for (int i = 0; i < 18 - a_len; i++){
arr_entry[i] = 0;
}
for (int index = 0; index < 18; index++){
arr_list[arr_index][index] = arr_entry[index];
cout << arr_entry[index];
cout << arr_list[arr_index][index];
}
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
arr_index ++;
}
/**
for (int c = 1; c < 5; c++){
if (max_len > (c-1)*5){
step(arr_list, c, T);
for (int i = 0; i < T; i++){
int is_leading_zero = 1;
for (int j = 0; j < 18; j++){
if (is_leading_zero == 0){
cout << arr_list[i][j];
}else{
if (arr_list[i][j] != 0){
is_leading_zero = 0;
cout << arr_list[i][j];
}
}
}
cout << " ";
}
cout << "\n";
}
}
**/
}
I'm assuming this is a common error, and that I'm missing something simple that gives me segfaults for values I already assigned data to.
Does anyone know why this is happening?
You are allocating 72 MB on the stack:
int main()
{
[...]
int arr_list[1000000][18];
[...]
}
This is probably causing a stack overflow.
On the Microsoft Windows platform, the maximum stack size is, by default, 1 MB. On Linux, it is typically 8 MB.
When allocating such large amounts of memory, I recommend that you instead either use
dynamic memory allocation, or
a global variable, or
a static local variable.
This ensures that the array is not stored on the stack.
I know this might be a duplicate to another question on this forum but I couldn't find the solution for my problem, even if I searched for like 1 hour.
The problem is that my program stops after the 4th "cin". I don't know why, I tried everything: "cin.ingore(); cin.clear();", "cin.get();".
Could someone help me please?
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
struct elev
{
char nume[20];
vector<int> note_info;
float medie;
};
int main()
{
int n, e = 0;
vector<elev> elevi;
cout << "n = "; cin >> n;
for (int i = 1; i <= n; i++)
{
int s = 0, nr;
elevi.push_back(elev());
cout << "Nume elev: "; cin >> elevi[i].nume;
cout << "Numar note informatica: "; cin >> nr;
for (int j = 0; j < nr; j++)
{
int temp;
cout << "Nota nr. " << j + 1 << ": "; cin >> temp;
elevi[i].note_info.push_back(temp);
s += temp;
}
elevi[i].medie = (float)(s / nr);
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
if (elevi[j].medie != elevi[j + 1].medie)
{
e += 1;
}
}
}
if (e)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].medie < elevi[j + 1].medie)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
else
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].nume > elevi[j + 1].nume)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
cout << "Rezultate:";
for (int i = 1; i <= n; i++)
{
cout << '\n' << elevi[i].nume << ' ' << setprecision(2) << fixed << elevi[i].medie;
}
return 0;
}
Replace this line:
for (int i = 1; i <= n; i++)
with
for (int i = 0; i < n; ++i)
The error stems from trying to access the vector elevi at a position it doesn't yet have. Because vectors start indexing at 0, the first access made to elevi should be at index 0.
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.