Hi guys I am kinda new on c++ so I was writing a program and it works fine, but there is a problem. Every time I type number bigger than 100 my program crashes and I don't know why. Could anyone help me?
Program code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i = 10;
while(i > 0)
{
i--;
int b = 0, c = 1, d = 0, e, number, how = 0, number1, start, to, number2, split1, split2, mass, start1 = 0, start2 = 0, number3, how1, number4, number5;
cout << "\nIveskite skaiciu \n";
cin >> number;
cout << "\n";
number1 = number;
while(number1 > 0)
{
number1 = number1 / 10;
how = how + 1;
}
how1 = how - 1;
start = pow(10, (how - 1));
to = pow(10, how);
mass = to - start;
number2 = start - 1;
int split[mass][mass], numbers[mass], ok[mass];
while(start1 < mass)
{
start1++;
e = number2 + start1;
numbers[start1] = e;
split[start1][0] = e;
}
while(start2 < mass)
{
start2++;
number3 = numbers[start2];
d = 0;
b = 0;
c = 1;
while(d <= how1)
{
d++;
split1 = number3%10;
split2 = number3 / 10;
number3 = split2;
split[start2][d] = split1;
number4 = b + split[start2][d];
b = number4;
number5 = c * split[start2][d];;
c = number5;
}
if(number4 == number5)
{
ok[mass] = numbers[start2];
cout << number4 << " " << number5 << " >" << ok[mass] << endl;
}
}
}
Seems to me its the 2D array split that gets too large for your stack. You could probably try to allocate it dynamically as suggested here: how to deal with large 2D arrays
Related
I am used to write C++ project in CodeBlocks, but for some stupid reasons I have to show it to my teacher in VisualStudio. I tried to make a console app or an empty project, and copied my main file there, but with the first one I get bunch of erorrs and the second one I get 'The system cannot find the way specified'. What is different in VisualStudio? I don't understand at all what is wrong.
here is my code
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
const int kroku = 1000;
const double aa = 0; //pocatecni bod intervalu
const double bb = 1; //konečný bod intervalu
double a; //parametr
const double h = (bb - aa) / kroku; //krok
double p(double t) { //(py')' - qy = f
return exp(a*pow(t, 2));
}
double q(double t) {
return -exp(a*pow(t, 2))*pow(a, 2)*pow(t, 2);
}
double dp(double t) {
return 2 * t*a*exp(a*pow(t, 2));
}
double y[kroku + 1]; //řešení původní rce
double dydx[kroku + 1];
double z[kroku + 1]; //řešení dílčí rce
double dzdx[kroku + 1];
double x[kroku + 1]; //rozdělení intervalu (aa, bb) po krocích h
void generateX() { //generuje hodnoty x
for (int k = 0; k <= kroku; k++) {
x[k] = aa + k*h;
}
}
double partial(double pp1, double pp2, double w[kroku + 1], double dwdx[kroku + 1], double v)//řešení rce (pw')' - qw = g s pp
{
w[v] = pp1; //inicializace - počáteční podmínka
dwdx[v] = pp2; //inicialzace - počáteční podmínka
for (int i = 0; i <= kroku; i++) { //substituce dwdx proměnná -> dwdx = (w_(n+1) - w_n)/h) && dwdx =
w[i + 1] = h*dwdx[i] + w[i];
dwdx[i + 1] = (h / p(aa + h*i))*(q(aa + h*i)*w[i] - dp(aa + h*i)*dwdx[i]) + dwdx[i];
}
return 0;
}
double omega1, omega2; //nové počáteční podmínky omega1 = y(x0), omega2 = y'(x0)
void print(double N[kroku + 1])
{
fstream file;
file.open("data.dat", ios::out | ios::in | ios::trunc);//otevření/vytvoření(trunc) souboru
if (file.is_open()) //zápis do souboru
{
cout << "Writing";
file << "#" << "X" << " " << "Y" << endl;
for (int j = 0; j <= kroku; j++) {
file << x[j] << " " << N[j] << endl;
}
file << "#end";
}
else
{
cout << "Somethinq went wrong!";
}
file.close();
}
int main()
{
double alpha; //pocatecni podminka y(aa) = alpha
double beta; //y(bb) = beta
cout << "Assign the value of beta " << endl;
cin >> beta;
cout << "Assign the value of alpha " << endl;
cin >> alpha;
cout << "Assign the value of parameter a" << endl;
cin >> a;
double alpha1 = 0; //alpha1*p(aa)*y'(aa) - beta1*y(aa) = gamma1
//double alpha2 = 0; //alpha2*p(bb)*y'(bb) + beta2*y(bb) = gamma2
double beta1 = -1;
double beta2 = 1;
double gamma1 = alpha;
double gamma2 = beta;
generateX();
partial(alpha1, beta1 / p(aa), z, dzdx, aa); //(pz')'-qz = 0
omega1 = gamma2 / beta2;
omega2 = 1 / (z[kroku] * p(bb))*(gamma1 + dzdx[kroku] * p(bb));
partial(omega1, omega2, y, dydx, aa);//(py')' - qy = f = 0
print(y);
return 0;
strong text}
when I add
#include "stdafx.h"
I get four errors
2x 'Expression must have integral or unscoped enum type'
2x 'subscript is not of integral type'
for these lines
w[v] = pp1;
dwdx[v] = pp2;
Could anyone please help me? Thank you a lot
array subscript v in your line
w[v]
can not be double. It must be of interger type.
I'm a beginner in programming. I am trying to make a program that given two numbers it substracts one harmonic from the other. (Input: n, m / Output: Hn-Hm)
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main() {
double n1, n2, h1 = 0, h2 = 0, i; // n = number, h = harmonic
cin >> n1 >> n2;
if (n1 == 0) {
h1 = 0;
}
else {
for (i = 1; i <= n1; i++) {
h1 += 1 / i;
if (i <= n2) {
h2 += 1 / i;
}
}
}
cout << fixed << setprecision(10) << h1 - h2 << endl;
system("pause");
return 0;
}
The program gives correct results but I'm using a website of my university and it says that the program is slow. I've tried to make it faster but I can't figure out how.
Thanks.
You don't need to calculate the full harmonic numbers. Assuming n1 < n2, the two series will be:
H(n1) = 1 + 1/2 + 1/3 + ... + 1/n1
H(n2) = 1 + 1/2 + 1/3 + ... + 1/n1 + 1/(n1+1) + 1(n1+2) + ... + 1/n2
So when you subtract H(n2) - H(n1), the first n1 terms in the two series cancel each other out, so
H(n2) - H(n1) = 1/(n1+1) + 1(n1+2) + ... + 1/n2
If n1 > n2 the result is the negative of this.
double result = 0, mult = 1;
if (n1 > n2) {
double temp = n1;
n1 = n2;
n2 = temp;
mult = -1;
}
for (double denom = n1+1; denom <= n2; denom++) {
result += 1/denom;
}
result *= mult; // Flip the sign if we swapped n1 and n2
cout << fixed << setprecision(10) << result << endl;
When I try to start my C++ program it stops with error "5.exe has stopped working". This program supposed to calculate how many tiles you need for pool, if number of tiles on one side is non-round number, add one row of tiles to it. P.S. Sorry for my bad English.
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
int main()
{
int x,y,z;
int a,b;
cout << "Insert dimensions of pool in metres: " << endl;
cin >> x >> y >> z;
cout << "Insert dimensions of tile in centimeters: " << endl;
cin >> a >> b;
a=a/100;
b=b/100;
int brx = 0, brzx = 0, bry = 0, brzy = 0, bxpod = 0, bypod = 0;
if (x%a == 0) {
brx = x / a;
}
else {
brx = x / a + 1;
}
if (z%b == 0) {
brzx = z / b;
}
else {
brzx = z / b + 1;
}
if (y%a == 0) {
bry = y / a;
}
else {
bry = y / a + 1;
}
if (z%b == 0) {
brzy = z / b;
}
else {
brzy = z / b + 1;
}
if (x%a == 0) {
bxpod = x / a;
}
else {
bxpod = x / a + 1;
}
if (y%b == 0) {
bypod = y / b;
}
else {
bypod = y / b + 1;
}
int s = (brx*brzx + bry*brzy) * 2 + bxpod*bypod;
cout << "You need " << s << "tiles." << endl;
system("pause");
return 0;
}
Using a debugger, you can easily find that you have a division by 0 in the following lline:
if (x%a == 0) {
brx = x / a;
}
You are doing an integer division on "a":
a = a / 100;
So if a is lower than 100, a will be 0. 10 / 100 = 0.1 = 0 when cast as int.
You should use double instead of int
So I am trying to use the hill cipher to encrypt my 3x3 matrix with a given key. It works correctly for the first value outputting n which it should, but then after that value I get large values and it never takes the mod of them. I added the cout statements to help me debug and see what's going wrong, but I still can't fix it. Also the second mod 26 is there because when I didn't have it there I was getting negative 13 instead of positive 13. This is a homework program, our key was given to us as numbers in case that is of any importance.
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
using std::endl;
using std::string;
void inverse_matrix();
string encryption(string x);
int main()
{
std::string one = "paymoremoney";
// inverse_matrix();
encryption(one);
system("pause");
return 0;
}
string encryption(string x)
{
int encrypted[4][4];
int key[3][3];
key[0][0] = 4;
key[0][1] = 9;
key[0][2] = 15;
key[1][0] = 15;
key[1][1] = 17;
key[1][2] = 6;
key[2][0] = 24;
key[2][1] = 0;
key[2][2] = 17;
int test = 0;
char str[] = "";
char c;
int length = (int)x.length();
for (int i = 0; i < length; i++)
{
x[i] = tolower(x[i]);
}
/*
while (str[test])
{
c = str[test];
putchar(tolower(c));
test++;
}
*/
int encrypt[4][4];
encrypt[0][0] = x[0];
encrypt[0][1] = x[1];
encrypt[0][2] = x[2];
encrypt[1][0] = x[3];
encrypt[1][1] = x[4];
encrypt[1][2] = x[5];
encrypt[2][0] = x[6];
encrypt[2][1] = x[7];
encrypt[2][2] = x[8];
encrypt[3][0] = x[9];
encrypt[3][1] = x[10];
encrypt[3][2] = x[11];
encrypted[0][0] = (key[0][0] * encrypt[0][0]) + (key[1][0] * encrypt[0][1]) + (key[3][0] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;
encrypted[0][1] = (key[0][1] * encrypt[0][0]) + (key[1][1] * encrypt[0][1]) + (key[2][1] * encrypt[0][2])%26;
encrypted[0][0] %= 26;
encrypted[0][2] = (key[0][2] * encrypt[0][0]) + (key[1][2] * encrypt[0][1]) + (key[2][2] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;
std::cout << encrypted[0][0];
std::cout << endl;
std::cout << encrypted[0][1];
std::cout << endl;
std::cout << encrypted[0][2];
std::cout << endl;
}
As this is homework, I'll point in the right direction rather than give a direct answer. You're assigning to encrypted[0][1]. What do you do with that value after assigning to it?
Here is my unaltered working code:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance << ". The amount with interest is: " << Total[i] << endl;
system("pause");
}
}
Here is what i need to do: You must add a function to your program called CalcInterest. This function will take as its ONLY parameter an Account, and return the Interest calculated as shown in Part 1. Your main program should now use this function instead, to generate the display as in Part 1.
This is what i tried:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int CalcInterest(Account);
struct Account { //declare struct outside of main
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int AccountNumber[MAXACCOUNTS] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
double Balance[MAXACCOUNTS] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
int DaysSinceDebited[MAXACCOUNTS] = { 20, 35, 2, 14, 5, 360, 1, 45 };
double Total[MAXACCOUNTS] = {};
//add your code here
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
CalcInterest(Account);
}
int CalcInterest(Account) {
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited > 30))
Total[i] = accounts[i].Balance * 1.06;
else Total[i] = accounts[i].Balance * 1.03;
cout << accounts[i].Number << "has a balance of " << accounts[i].Balance << ". The amount with interest is : " << Total[i] << endl;
}
system("pause");
return 0;
}
There were many errors with this, mostly of things becoming undefined, such as .DaysSinceDebited etc PLEASE HELP!
I believe that by "there are many errors", you are talking about compilation errors. A quick look at your code confirms this.
The first mistake you made is that this function only works on a single account. You therefore cannot loop over your accounts array inside that function, neither can you access Total. You are also a little confused about the syntax of passing arguments, as well as the datatype that should be returned. I can help you with that.
Change your function definition to:
double CalcInterest( const Account & account )
{
// Do your interest calculation on 'account' here, then return it from the function.
double interest = 0.0; //<-- For you to do.
return interest;
}
Then you can simplify your loop in main...
for (int i = 0; i < MAXACCOUNTS; i++)
{
// Calculate the interest on the account, then do something with it.
double interest = CalcInterest( accounts[i] );
Total[i] = 0.0; //<-- For you to do.
}
Note that I've only provided the language structure here, since this is obviously an assignment of some sort. I have indicated the parts where you need to do some work.