How do I print these two images next to each other and not one above another? - netbeans-8

I have been able to print both images one on top of another but I also need to print them beside each other. Can someone please help me? (I need to be able to print it under what I have already got)
Code for Square and triangle one on top of another
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" *");
}
System.out.println("");
}
System.out.println("");
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5-i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(" *");
}
System.out.print("\n");
}

Simply combine the two outermost for loops:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" *");
}
for (int j = 1; j <= 5 - i+1; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i+1; k++) {
System.out.print(" *");
}
System.out.print("\n");
}
System.out.println("");

Related

Detect break statement in function?

I have the function below:
- (void)doStuff {
BOOL allDoneNow = NO;
for (int i = 0; i < [arABFBmatches count]; i++) {
for (int j = 0; j < [arABFBmatches count]; j++) {
if (should_skip_rest) {
allDoneNow = YES;
break;
}
}
if (allDoneNow) break;
}
}
Now, if I call that method with [self doStuff];, how can I detect if the function broke? Or is there a better way to stop and restart the execution of a function?
- (BOOL)doStuff // <- return a boolean value
{
for (int i = 0; i < [arABFBmatches count]; i++)
{
for (int j = 0; j < [arABFBmatches count]; j++)
{
if (should_skip_rest)
{
return NO; // <- NO for break
}
}
}
return YES; // <- YES for completed
}
This beaks the execution of the function. If you want to restart it, simply call it in a while loop.
while( (doStuff()==NO) && thereShouldBeAnotherConditionForStopping )
{
// Do something after each attempt, otherwise it seems to be a little bit silly
}
Or you can use blocks, like this:
- (void)doStuff:(void (^)())breakBlock{
BOOL allDoneNow = NO;
for (int i = 0; i < [arABFBmatches count]; i++) {
for (int j = 0; j < [arABFBmatches count]; j++) {
if (should_skip_rest) {
allDoneNow = YES;
breakBlock();
break;
}
}
if (allDoneNow) breakBlock();
}
}
and call it:
[self doStuff:^{
NSLog(#"ends with break");
}];

How to pass C-style matrix in Objective-C

I trying to pass matrix to my function:
int movesMatrix[self.map.width][self.map.height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
movesMatrix[x][y] = -1;
}
}
- (void)foo:(int **)movesMatrix
{
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
NSLog(#"%d,%d - %d", y, x, movesMatrix[x][y]);
}
}
}
But I getting error:BAD_ACCESS
What am I doing wrong?
try to change to following:
- (void)foo:(int *)movesMatrix
...
NSLog(#"%d,%d - %d", y, x, movesMatrix[x*self.map.height + y]);

Dynamic two dimensional C array crashing in Objective-C

I am making a curve fitting program using matrices in the form of a two dimensional arrays but the compiler throws out BAD_ACCESS errors at random such as Thread 1:EXC_BAD_ACCESS (code=1, address=0x13f800000). The program works sometimes and other times crashes. Any ideas would be appreciated. Thanks.
- (void)main {
NSLog(#"Enter size");
scanf("%i", &matrixSize);
float* temp;
matrix = (float**)calloc(matrixSize, sizeof(float*));
temp = (float*)calloc(matrixSize+1, sizeof(float));
for (int i = 0; i < matrixSize+1; i++) {
matrix[i] = temp + (i*(matrixSize+1));
}
[self enterPoints];
[self elimination];
[self setNeedsDisplay:YES];
free(matrix);
free(temp);
}
//points entered here
- (void)enterPoints {
CGPoint *points = (CGPoint *)malloc(matrixSize * sizeof(CGPoint));
for (int i = 0; i < matrixSize; i++) {
scanf("%lf", &points[i].x);
scanf("%lf", &points[i].y);
}
for (int j = 0; j < matrixSize; j++) {
for (int i = 0; i < matrixSize+1; i++) {
if (i == (matrixSize)) {
matrix[i][j] = points[j].y;
}
else {
matrix[i][j] = pow(points[j].x, (matrixSize-1)-i);
}
}
}
free(points);
}
//matrix reduction occurs here
- (void)elimination {
for (int j = 0; j < matrixSize; j++) {
double divideValue = matrix[j][j];
for (int i = 0; i < matrixSize+1; i++) {
matrix[i][j] /= divideValue;
}
for (int j1 = 0; j1 < matrixSize; j1++) {
if (j1 == j) {
if (j1 == matrixSize-1) {
break;
}
else {
j1++;
}
}
double subValue = matrix[j][j1];
for (int i = 0; i < matrixSize+1; i++) {
matrix[i][j1] -= matrix[i][j]*subValue;
}
}
}
}
//drawing the polynomial
- (void)drawRect:(NSRect)dirtyRect {
NSGraphicsContext * GraphicsContext = [NSGraphicsContext currentContext];
CGContextRef context = (CGContextRef) [GraphicsContext graphicsPort];
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextMoveToPoint(context, 0, matrix[matrixSize][0]*100 + 100);
[GraphicsContext saveGraphicsState];
CGMutablePathRef path;
path = CGPathCreateMutable();
for (float i = -matrixSize; i < matrixSize; i+=.01) {
float y = 0;
for (int j = 0; j < matrixSize; j++) {
y += matrix[matrixSize][j]*pow(i, j);
}
CGContextAddLineToPoint(context, i*100 + 100, y*100 + 100);
}
CGContextStrokePath(context);
[GraphicsContext restoreGraphicsState];
}
You did not allocate enough memory for your matrix. This line sets up the entire data area, but you have only allocated matrixSize+1 elements, instead of matrixSize*(matrixSize+1):
temp = (float*)calloc(matrixSize+1, sizeof(float));
So, maintaining the matrixSize+1 columns and matrixSize rows:
matrix = (float**)calloc(matrixSize, sizeof(float*));
temp = (float*)calloc(matrixSize * (matrixSize+1), sizeof(float));
for (int i = 0; i < matrixSize; i++) {
matrix[i] = temp + (i*(matrixSize+1));
}
When you use this later, be careful. You are addressing it wrong:
for (int j = 0; j < matrixSize; j++) {
for (int i = 0; i < matrixSize+1; i++) {
if (i == (matrixSize)) {
matrix[i][j] = points[j].y;
}
else {
matrix[i][j] = pow(points[j].x, (matrixSize-1)-i);
}
}
}
Notice that i goes to matrixSize+1 but you are using that as the row index (there are only matrixSize rows). I think you meant to use matrix[j][i] instead of matrix[i][j]. You also do this when you construct the initial matrix, but I've actually changed that to be in line with your allocation.
So there are two points of buffer overrun in your program that I see.
EXC_BAD_ACCESS indicates one of your objects is being over-released (not to be confused with garbage collection) before invoking a method on it. Once you reach the point in your code where you invoke the method on the collected object, the pointer is referencing an invalid memory location.
To find Zombie objects have a look at this: How to Enable NSZombie in XCode
I've used this and it works very well.

Converting Objective C to C Matrix Manipulation

Okay, so I had working code written in all objective c (yes, I know that objc is technically just C. But i mean I had it written with messages and stuff. I only have a java background and do not know much about plain old C) but it ran incredibly slow. So I wrote out (what i thought) was the same code, but now this set of loops produces different values (for only some of the numbers) and I cannot, for the life of me, figure out whats different. What I am doing is looping 10 times and doing 1 multiplication and 1 addition between matricies. I'm hoping someone with more background with the two languages can pick out the part of code that I transcribed incorrectly. I did not change anything beforehand for any of the arrays (those were hardcoded in and uneffected) so A1, A2, etc have the same values in both parts of code.
Current code in C:
for (int m = 0; m < 10; m++) {
//Do matrix multiplication between A1 and A2. Store in temporary B1
for( int i = 0; i < 13; i++ )
for( int j = 0; j < 43; j++ ) {
double tempTotal = 0;
for( int k = 0; k < 43; k++){
tempTotal = tempTotal + A1[i][k] * A2[k][j];
}
B1[i][j] = tempTotal;
}
//Assign B1 data back into A1 after the multiplication is finished
for(int i = 0; i < 13; i++)
for(int j = 0; j<43; j++)
A1[i][j] = B1[i][j];
//Add C1 and A1. Store into C1.
for (int l = 0; l < 13; l++)
for (int n = 0; n < 43; n++)
C1[l][n] = C1[l][n] + A1[l][n];
}//end m for loop
This was the old Obj c code:
for (int m = 0; m < 10; m++) {
//multiply A1 and A2. Store into A1
A1 = [LCA_Computation multiply:A1 withArray:A2]; //LCA_Computation is the name of the .m class file in which this all happens.
//Add C1 and A1. Store into C1
for (int i = 0; i < 13; i++)
for (int j = 0; j < 43; j++)
[[C1 objectAtIndex:i] replaceObjectAtIndex:j withObject:[NSNumber numberWithDouble: [[[C1 objectAtIndex: i] objectAtIndex: j] doubleValue] + [[[A1 objectAtIndex: i] objectAtIndex: j] doubleValue]]];
}//end m for loop
//multiply method
+ (NSMutableArray*)multiply:(NSMutableArray*)a1 withArray:(NSMutableArray*)a2
{
int a1_rowNum = [a1 count];
int a2_rowNum = [a2 count];
int a2_colNum = [[a2 objectAtIndex:0] count];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:a1_rowNum];
for (int i = 0; i < a1_rowNum; i++) {
NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:a2_colNum];
for (int j = 0; j < a2_colNum; j++) {
double tempTotal = 0;
for (int k = 0; k < a2_rowNum; k++) {
double temp1 = [[[a1 objectAtIndex:i] objectAtIndex:k] doubleValue];
double temp2 = [[[a2 objectAtIndex:k] objectAtIndex:j] doubleValue];
tempTotal += temp1 * temp2;
}
//the String format is intentional. I convert them all to strings later. I just put it in the method here where as it is done later in the C code
[tempRow addObject:[NSString stringWithFormat:#"%f",tempTotal]];
}
[result addObject:tempRow];
}
return result;
}
This issue had to do with prior memory management issues causing 0's to be used in some calculations.

Defining a matrix as an array of arrays and computation its inverse matrix in C++

Unfortunately I haven't much experience in C++ and I'm trying to progress myself in C++.
Firstly,I defined array of arrays so that I formed an 3x3 matrix:
array< array< double >^ >^ input = gcnew array< array< double >^ >(3);
for (j=0;j<input->Length;j++){
input[j]=gcnew array<double>(3);
Then I assigned matrix elements to input array of arrays:
int value=1;
for(y=0;y<(3);y++){
for(x=0;x<(3);x++)
{input[y][x]=value;
value=value+1;
}
}
Is there a C++ function that compute inverse matrix of this input array of arrays?
Could you help me please?
Best Regards...
Look at Simple 3x3 matrix inverse code (C++).
There are no functions in C++ to make matrix operations, you'll need to find some library to do it or implement your own.
Note that for fixed size arrays, you can use regular C/C++ arrays, like this one:
double arr[3][3];
Wikipedia has a list of numerical libraries in various programming languages. If the functions that you are looking for are not available you could also consider writing a wrapper function around an available Fortran or C or C++ implementation.
There is not built in C++ function to do matrix inversion (or for any other matrix calculations).
There are lots of methods for matrix inversion mentioned HERE.
If you want efficiency and ease of implementation then Guassian Elimination is
probably the best.
Code for inverse of matrix using Elementary row transformation in c++
#include<iostream>
#include<stdio.h>
#include<conio.h>
using
namespace std;
float a[4][4];float b[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
int no = 4;
int check(int k) {
float cont = 0, cont2 = 0;
for (int i = k; i < no; i++) {
if (a[i][k] == 1) {
for (int j = 0; j < no; j++) {
cont = a[i][j];
cont2 = b[i][j];
a[i][j] = a[k][j];
b[i][j] = b[k][j];
a[k][j] = cont;
b[k][j] = cont2;
}
} else if (a[i][k] == 0) {
for (int j = 0; j < no; j++) {
cont = a[i][j];
cont2 = b[i][j];
a[i][j] = a[no - 1][j];
b[i][j] = b[no - 1][j];
a[no - 1][j] = cont;
b[no - 1][j] = cont2;
}
}
}
return 0;
}
int divi(int k) {
float particular = a[k][k];
for (int i = 0; i < no; i++) {
a[k][i] = a[k][i] / particular;
b[k][i] = b[k][i] / particular;
if (a[k][i] == (-0)) {
a[k][i] = 0;
}
if (b[k][i] == (-0)) {
b[k][i] = 0;
}
}
return 0;
}
int sub1(int k) {
float particular;
for (int j = k + 1; j < no; j++) {
particular = a[j][k];
for (int i = 0; i < no; i++) {
a[j][i] = a[j][i] - (particular * a[k][i]);
b[j][i] = b[j][i] - (particular * b[k][i]);
if (a[j][i] == (-0)) {
a[j][i] = 0;
}
if (b[j][i] == (-0)) {
b[j][i] = 0;
}
}
}
return 0;
}
int sub2(int k) {
float particular;
for (int j = k - 1; j >= 0; j--) {
particular = a[j][k];
for (int i = no - 1; i >= 0; i--) {
a[j][i] = a[j][i] - (particular * a[k][i]);
b[j][i] = b[j][i] - (particular * b[k][i]);
if (a[j][i] == (-0)) {
a[j][i] = 0;
}
if (b[j][i] == (-0)) {
b[j][i] = 0;
}
}
}
return 0;
}
int disp(){
cout<<endl;
for(int x=0;x<no;x++){
for(int y=0;y<no;y++){
if(a[x][y]==(-0)){a[x][y]=0;}
if(b[x][y]==(-0)){b[x][y]=0;}
printf("%0.1f|||%0.1f ",a[x][y],b[x][y]);
}
cout<<endl;}
}
int main()
{
for(int i=0;i<no;i++){
for(int j=0;j<no;j++){cout<<"Enter a["<<i<<"]["<<j<<"]";cin>>a[i}[j];}
}
for(int i=0;i<no;i++){
for(int j=0;j<no;j++){cout<<a[i][j]<<" ";}
cout<<endl;
}
for(int i=0;i<no;i++){
check(i);
disp();
divi(i);
disp();
sub1(i);
disp();
}
for(int i=no-1;i>=0;i--){
sub2(i);
disp();
cout<<endl;
}
getch();
getch();
getch();
return 0;}
}