C++ Functions in loop/struct - c++

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.

Related

C++ Code segmentation fault only in vscode

My C++ code (shown below) works on this site:
GDB Online but not in Visual Studio, where it crashes at
iterations[imag_times][real_times] = i % (iter / 2);
when imag_times is 1 and real_times is 0 with the exception being Exception has occurred. Segmentation fault
I have installed GDB version 7.6.1.
My Question: Does anybody know how to fix that and why this is happening?
#include <iostream>
using namespace std;
int main()
{
// initialization
const double real_min = -1;
const double real_max = 1;
const double imag_min = -1;
const double imag_max = 1;
const int iter = 30;
const double real_offs = 0.01;
const double imag_offs = 0.01;
double z_real = 0;
double z_imag = 0;
double c_real = real_min;
double c_imag = imag_max;
int real_times = 0;
int imag_times = 0;
int** iterations = new int*[1];
iterations[0] = new int;
int i = 0;
// start
while(c_imag >= imag_min)
{
iterations = (int**)realloc(iterations, sizeof(int*) * (imag_times + 1));
real_times = 0;
c_real = real_min;
while(c_real <= real_max)
{
iterations[imag_times] = (int*)realloc(iterations[imag_times], sizeof(int) * (real_times + 1));
z_real = 0;
z_imag = 0;
for(i = 0; i < iter; i++)
{
double z_imag2 = z_imag * z_imag;
z_imag = 2 * z_real * z_imag + c_imag;
z_real = z_real * z_real - z_imag2 + c_real;
if(z_real * z_real + z_imag * z_imag > 4)
{
break;
}
}
iterations[imag_times][real_times] = i % (iter / 2);
real_times++;
c_real = real_min + real_offs * real_times;
}
imag_times++;
c_imag = imag_max - imag_offs * imag_times;
}
// output
for(int i = 0; i < imag_times; i++)
{
for(int j = 0; j < real_times; j++)
{
cout << iterations[i][j];
cout << ",";
}
cout << "\n";
}
cout << "done";
std::cin.get(); // pause so the program doesnt exit instantly
return 0;
}
Thanks in advance!

C++ loop not working as expected

I want to plot where the x and y variables are in an array, and when the x or y value is greater than its respective dimension in the array, they should change direction. However, when I run the program the Y value keeps going up. I am new to C++ so any help is greatly appreciated. Here is my code:
#define PI 3.14159265
#include <iostream>
#include <tgmath.h>
int timeRun = 0;
int rect[500][1000] = {0};
int theta = 50;
int x = 0;
float y = 0;
float previousY = 0;
int yGo;
int dir = 0;//0 = right; 1 = left;
int main()
{
for(int a = 30; a<=89; a=a+1){
memset(rect,0,sizeof(rect));
x = 0;
y = 1;
theta = a;
std::cout << theta;
int sum = 0;
for(int t = 0; t<1000;t=t+1){
y = previousY + tan(theta * PI/180);
previousY = y;
yGo = floor(y);
rect[x][yGo] = 1;
if(dir==0){
x++;
}
if(dir==1){
x--;
}
if(x>499 && dir==0){
dir = 1;
if(theta%360 >= 270 && theta%360 <= 360){
theta+=(a-180);
}
}
if(x<1 && dir==1){
dir = 0;
if(theta%360 >= 0 && theta%360 <= 90){
theta+=(180-a);
}
}
if(y>998 && dir ==0){
theta+=(a-180);
}
if(y>998 && dir ==1){
theta+=(180-a);
}
if(y<1 && dir ==0){
theta+=(180-a);
}
if(y<1 && dir ==1){
theta+=(a-180);
}
}
for ( int i = 0; i < 500; i++ ){
for ( int j = 0; j < 1000; j++ ){
sum+=rect[i][j];
}
}
std::cout << sum;
}
}
Thank you for any help!
I really don't understand your program. I simplified the core to:
static const float radian_conversion = 3.14159264f / 180.0f;
int x = 0;
float y = 0.0f;
int theta = 30;
int dir_add = 1;
cout << "t | x | y | theta" << endl;
const float y_increment = tan(theta * radian_conversion);
for (int t = 0; t < 1000; ++t)
{
y = y + y_increment;
cout << t << "|" << x << "|" << y << "|" << theta << "\n";
int y_index = floor(abs(y));
rect[x][y_index] = 1;
x = x + dir_add;
if ((x > 499) || (x < 1))
{
dir_add = dir_add * -1;
}
}
I'm also showing how you can make the x variable increment and decrement.
The statements that don't change or don't cause a variable to change have been extracted out of the loop.
I recommend you take the output of the above program into a spreadsheet program and have the spreadsheet program plot it.

Hill Cipher Encrypt

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?

C++ Program Crashing

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

SSE addition producing garbage

I am trying to compare SSE float[4] addition to standard float[4] addition. I tried this:
#include <iostream>
#include <vector>
struct Point4
{
Point4()
{
data[0] = 0;
data[1] = 0;
data[2] = 0;
data[3] = 0;
}
float data[4];
};
static float SumOfDifferences(const Point4& a, const Point4& b)
{
// This function only returns the sum of the sum of the components
float sumValues = 0.0f;
for(unsigned int i = 0; i < 4; ++i)
{
sumValues += a.data[i] + b.data[i];
}
return sumValues;
}
void Standard()
{
Point4 a;
a.data[0] = 1;
a.data[1] = 2;
a.data[2] = 3;
a.data[3] = 4;
Point4 b;
b.data[0] = 1;
b.data[1] = 6;
b.data[2] = 3;
b.data[3] = 5;
float total = 0.0f;
for(unsigned int i = 0; i < 1e6; ++i)
{
total += SumOfDifferences(a, b);
}
std::cout << "total: " << total << std::endl;
}
void Vectorized()
{
typedef int v4sf __attribute__ (( vector_size(4*sizeof(float)) ));
v4sf a;
float* aPointer = (float*)&a;
aPointer[0] = 1; aPointer[1] = 2; aPointer[2] = 3; aPointer[3] = 4;
v4sf b;
float* bPointer = (float*)&b;
bPointer[0] = 1; bPointer[1] = 2; bPointer[2] = 3; bPointer[3] = 4;
float total = 0.0f;
v4sf result;
float* resultPointer = (float*)&result;
for(unsigned int i = 0; i < 1e6; ++i)
{
result = a + b; // Vectorized operation
// Sum the components of the result (this is done with the "total += " in the Standard() loop
for(unsigned int component = 0; component < 4; ++component)
{
total += resultPointer[component];
}
}
std::cout << "total: " << total << std::endl;
}
int main()
{
// Standard();
Vectorized();
return 0;
}
but the output is 'inf' for the Vectorized() function. When I stepped through with a debugger, the values of 'result' seem to be garbage (i'd expect them to be (0, 4, 0, 1) ). Where am I going wrong here?
Try typedef float v4sf __attribute__ (( vector_size(4*sizeof(float)) ));
I get 2e+07 as the result.