Trying to reverse/invert display of digit pyramid [duplicate] - android

This question already has answers here:
Print reverse pyramid of numbers
(5 answers)
Closed 6 years ago.
My code below prints the following.
1
123
12345
1234567**
I want to to display this instead, but not sure what I'm doing wrong.
1234567
12345
123
1**
int a = 1;
int b = t5;
for (int i = 1 ; i <= t5 ; i++) {
for (int c = 1 ; c <= b - 1 ; c++) {
text.append(" ");
}
for (int k = 1 ; k <= a ; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}

this is my answer i solved it
int b = t5 ;
int a = 1;
for (int i = b + 3 ; i > 0; i--) {
for (int c = 1 ; c <= a - 1 ; c++){
text.append(" ");
}
for (int j = 1; j <= i ; j++) {
String result = String.valueOf(j);
text.append(result);
}
i = i - 1;
a++;
b--;
text.append("\n");
}

Related

Encoding ASCII in c++ for android application

I have some C++ code that I use in my android application. It takes a string and converts each character to an int from this ascii table. I then renders the correct glyph based on this.
It works on most of the devices I tested a few different google pixels and samsung phones, but when I test in a 3.3" WQVGA emulator on api 24 - 30 it doesn't function properly.
I imagine I'm going at this all wrong. Is there a better way of doing this? Is there a fix to my code?
Thanks for your time.
void text::drawtexthorizontal( double x, double y, int textheight, string textin)
{
int n = textin.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the string to char array
strcpy(char_array, textin.c_str());
for (int i = 0; i < n; i++){
//get ascii or "extended" ascii index and store it in symbol
int symbol;
if( (char_array[i] & ( 1 << 7 )) >> 7 == 1){
if( (char_array[i] & ( 1 << 6 )) >> 6 == 1){
//comb takes 2 byte representation and returns an int between 128 and 255 corresponding to extended ascii
symbol = comb(int(char_array[i]),int(char_array[i+1]));
i++;
}
else{
symbol = comb(int(char_array[i+1]),int(char_array[i]));
i++;
}
}
else symbol = int(char_array[i]);
//render chars[symbol]
}
}
int text::comb( int n, int m){
/*https://naveenr.net/unicode-character-set-and-utf-8-utf-16-utf-32-encoding/ for 2 byte encoding*/
int a[8] = { 0 },b[8] = { 0 };;
int i,k=0;
for (i = 0; n > 0; i++) {
a[i] = n % 2;
n /= 2;
}
for (i = 0; m > 0; i++){
b[i] = m % 2;
m /= 2;
}
for (i = 4; i >=0 ; i--){
k = 10 * k + a[i];
}
for (i = 5; i >=0 ; i--){
k = 10 * k + b[i];
}
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = k;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
if(dec_value > 255) dec_value = 32;
return dec_value;
}

How to print a full pyramid pattern?

My expected output
I tried the following code, but it prints only one *. I am confused where the code has went wrong.
int rows = 6, k = 0;
for(int i = 1; i <= rows; ++i, k = 0) {
for(int space = 1; space <= rows - i; ++space) {
t2.setText(" ");
}
while(k != 2 * i - 1) {
t2.setText("* ");
++k;
}
t2.setText();
}
This will work like a charm...
int y = 5,x = 5;
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x-i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i+1; k++)
{
System.out.print("* ");
}
System.out.print("\n");
}
Try This:
```
Scanner sc = new Scanner(System.in);
char s;
int l;
System.out.println("Enter the Letter : ");
s = sc.next().charAt(0);
l = Character.getNumericValue(s)-9;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < (l - i); j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.printf("%c%s",(char)j+65,"");
for (int j = i; j > 0; j--)
System.out.printf("%c%s",(char)j+64,"");
System.out.println();
}
```
The OP could call this:
String s = createCharPyramid(5, "*");
t2.setText(s);
and define this method:
private String createCharPyramid(int numberOfRows, String mySymbol){
String s = "";
String sep = System.lineSeparator();
for(int i = 0; i < numberOfRows; i++){
for(int j = 0; j < (numberOfRows - i); j++){
s = s + " ";
}
for(int j = 0; j <= i; j++){
s = s + mySymbol;
}
for(int j = i; j > 0; j--){
s = s + mySymbol;
}
s = s + sep;
}
return s;
}
This answer is based on MrRobot's answer. His answer is a pure java solution and would correctly show a pyramid in the console. So, if you like this answer give him a +1.

Print reverse pyramid of numbers

I am able to print pyramid like this :
1
123
12345
1234567
Code i used to print the pyramid of numbers like above is :
int a = 1;
int b = 4;
for (int i = 1 ; i <= 4 ; i++){
for (int c = 1 ; c <= b - 1 ; c++){
text.append(" ");
}
for (int k = 1 ; k <= a ; k++){
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}
but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :
1234567
12345
123
1
Any help would be appreciated?
try this ...
for (int i = 7; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");
if (i % 2 != 0) {
i = i - 1;
}
}
Try this
int a = 1;
int b = 4;
for (int i = 1; i <= 4; i++) {
for (int k = a; k >= 1; k--) {
String result = String.valueOf(k);
text.append(result);
}
for (int c = 1; c <= b - 1; c++) {
text.append(" ");
}
a = a + 2;
b--;
text.append("\n");
}
System.out.println(text.reverse());
Try this :)
int a = 1;
int b = 4;
int m = 4;
for (int i = 1; i <= 4; i++) {
for (int c = 1; c <= m; c++) {
text.append(" ");
}
for (int k = 1; k <= b; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
m++;
text.append("\n");
}
int numberofdigits = 7;
int numberofdigitsforrow = 0;
int Emptyspace = 0;
for (int i = 0; i < numberofdigits; i++)
{
numberofdigitsforrow = numberofdigits - i * 2;
if (numberofdigitsforrow > 0)
{
Emptyspace = numberofdigits-numberofdigitsforrow;
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace/2; b++)
{
Console.Write(" ");
}
}
for (int c = 1; c <= numberofdigitsforrow; c++)
{
Console.Write(c.ToString());
}
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace / 2; b++)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
int w = 7;
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= 5; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = 1; j <=w; j++)
{
System.out.print(j+" ");
}
w = w - 2;
System.out.println();
}

Android: How to generate unique random numbers in an array and perform bubble sort?

The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}

Fast Fourier transform of the recorded wav file in Android

There is a wav file on SD card. First, I connect to a file and reads the byte:
File file = null;file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/202.wav");
byte[] byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();}
I then use the transformation of data from a byte type in type double, because the FFT procedure does not work with byte type:
double[] transformed = new double[(int) file.length()];
for (int j=1;j<8194;j++) {
transformed[j] = (double)byteData[j]; }
And then the procedure of FFT processing this data:
public void spektr(double[] x, double[] y, int Dim, int D, double[] xx, double[] yy) {
int I,J,N,L,K,LE,LE1,IP,NV2,NM1;
double Arg,U1,U2,U3,C,S,T1,T2,T3,T4;
N = (int) Math.pow(2,Dim);
Log.v("N", "N "+N);
for (L = 1; L < Dim; L++){
LE = (int) Math.pow(2,Dim+1-L);
LE1 = LE/2;
U1 = (double)1.0;
U2 = (double)0.0;
Arg = (double)Math.PI/LE1;
C = (double)Math.cos(Arg);
S = D*(double)Math.sin(Arg);
for(J = 1; J < LE1; J++){
I = J;
do{
IP = I+LE1;
T1 = x[I] + x[IP];
T2 = y[I] + y[IP];
T3 = x[I] - x[IP];
T4 = y[I] - y[IP];
x[IP] = T3*U1 - T4*U2;
y[IP] = T4*U1 + T3*U2;
x[I] = T1;
y[I] = T2;
I = I + LE;
}while(I <= N);
U3 = U1*C - U2*S;
U2 = U2*C - U1*S;
U1 = U3;
}
}
NV2 = N / 2;
Log.v("NV2", "NV2 "+NV2);
NM1 = N-1;
Log.v("NM1", "NM1 "+NM1);
J = 1;
for(I = 1; I < NM1; I++){
if (I < J){
T1 = x[J];
T2 = y[J];
x[J] = x[I];
y[J] = y[I];
x[I] = T1;
y[I] = T2;
}
K = NV2;
while (K < J){
J = J - K;
K = K / 2;
};
J = J + K;
}
for(I = 1; I < N; I++){
x[I] = x[I] / N*2;
y[I] = y[I] / N*2;
}
for(I = 1; I < N; I++){
xx[I] = x[I];
yy[I] = y[I];
}
xx[1] = (double)0.0;
}
Reads the file successfully, but the processing procedure of the FFT is not working.
How to find the FFT procedure, or give the code to check the FFT result?
here is some free library to do FFT in java.

Categories

Resources