Simplification of For Loop with different inputs - loops

I would like to know if anyone of you has an idea of how I could choose the values in order to not to have the for loop repeated for every input. Thank you in advance.
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, e, f, g, h, i;
std::cout << "Please enter a number from 1 to 8: ";
std::cin >> i;
std::cout << "The value you entered is " << i;
std::cout << "\n";
if (0 <= i <= 8) {
if (i == 8) {
for (a = 1; a <= 8; a++)
{
std::cout << "\n";
for (c = 7; c >= a; c--) {
std::cout << " ";
}
for (b = 1; b <= a; b++) {
std::cout << "#";
}
}
}
else if (i == 7) {
for (a = 2; a <= 8; a++)
{
std::cout << "\n";
for (c = 7; c >= a; c--) {
std::cout << " ";
}
for (b = 1; b <= a; b++) {
std::cout << "#";
}
}
}
else if (i == 2) {
for (a = 7; a <= 8; a++)
{
std::cout << "\n";
for (c = 7; c >= a; c--) {
std::cout << " ";
}
for (b = 1; b <= a; b++) {
std::cout << "#";
}
}
}
}
return 0;
}
Here I would like to put all the loops together, meaning I dont have to type in the same loop for every integer

First, let me point out that this does not mean what you think it does:
if (0 <= i <= 8)
In C++, you need to write it like this:
if (0 <= i && i <= 8)
Now on to your loops. You wrote out three loops, for the cases where i is 8, 7, and 2, but I assume you also wants loops for the other acceptable values of i.
The only differences between your loops are what i must be and what a starts at:
i
initial a
8
1
7
2
2
7
What is the relationship between i and the initial a here? I notice that i + a == 9 in all three cases. So we can compute a = 9 - i.
if (0 <= i && i <= 8) {
for (a = 9 - i; a <= 8; a++) {
std::cout << "\n";
for (c = 7; c >= a; c--) {
std::cout << " ";
}
for (b = 1; b <= a; b++) {
std::cout << "#";
}
}
}

Related

Trouble reading string text into an array and counting letters

I am tasked to write a letter histogram which should be able to read any text and count the letters and output them in a neat way.
My question for this code so far is how to read the text into an array, I am stuck. I use two functions for this, one to read the text string into an array, and another function to output the array in this manner:
A: x
B: x
C: x
n: x
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
const int ANTAL_BOKSTAVER = 26;
void berakna_histogram_abs ();
void skriv_histogram_abs (int bokstav, int antal);
int main()
{
int i;
char alfabet;
string text;
cout << "Skriv in texten: ";
getline (cin, text);
void berakna_histogram_abs();
void skriv_histogram_abs();
return 0;
}
void berakna_histogram_abs ()
{
string text;
char str[100]= {getline};
int i= 0, alfabet[26]={0}, j;
while (str[i]!='\0')
{
if (str[i] >= 'a' && str[i]<='z')
{
j = str[i] - 'a';
++alfabet[j];
}
i++;
}
}
void skriv_histogram_abs()
{
int alfabet[26]={0};
int i;
cout << "Frekvensen av alla bokstäver är: " <<endl;
for (i=0; i < 26; i++)
cout << char(i + 'a')<<" : " << alfabet[i] << endl;
}
One can create and pass an array as below.
Create the array once.
Pass it as parameter. The variable and parameter names may differ.
So I renamed the text variable as demonstration.
For a histogram one needs to scale the length.
int main()
{
int i;
string in_text;
cout << "Skriv in texten: ";
getline (cin, in_text);
int alfabet[26] = {0};
berakna_histogram_abs(in_text, alfabet);
skriv_histogram_abs(alfabet);
return 0;
}
void berakna_histogram_abs (string text, int* alfabet)
{
}
void skriv_histogram_abs(int* alfabet)
{
int maxfreq = 0;
int i;
for (i=0; i < 26; i++)
if (alfabet[i] > maxfreq)
maxfreq = alfabet[i];
double ratio = 60.0 / (maxfreq == 0 ? 1 : maxfreq);
cout << "Frekvensen av alla bokstäver är: " <<endl;
for (i=0; i < 26; i++)
{
cout << char(i + 'a')<<" : ";
int laengd = (int)(alfabet[i] * ratio);
for (int j = 0; j < laengd; ++j)
cout << '■';
cout << " " << alfabet[i] << endl;
}
}
Arrays are generally passed as pointers.
I scaled the bar to some 60 characters, so the line length is kept below 80.

How do I change the bytes of a 32-bit integer?

I'm trying to add two binary numbers together using only logical statements and binary arithmetic operators. But I'm confused on how to actually change the bits. It is mostly the out variable that I am trying to change, but it keeps getting zeroed every time I print it.
#include <stdio.h>
void execute_add(int a, int b){
int i = 0;
int bit;
int bit2;
int carryOut = 0;
int out = 10;
int overflow = 0;
for(i = 0; i <32 ; i++){
bit = (a >> i) & 1;
bit2 = (b >> i) & 1;
if(bit==1 && bit2==1 && carryOut == 0){
carryOut = 1;
out = 0 | (0x1 >> i);
}else if(bit==1 && bit2==1 && carryOut == 1){
carryOut = 1;
out = 1 | (0x1 >> i);
}else if(bit==0 && bit2==0 && carryOut == 0){
carryOut = 0;
out= 0 | (0x1 >> i);
}else if(bit==0 && bit2==0 && carryOut == 1){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==1 && bit2==0 && carryOut == 0){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==1 && bit2==0 && carryOut == 1){
carryOut = 1;
out = 0 | (0x1 >> i);
}else if(bit==0 && bit2==1 && carryOut == 0){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==0 && bit2==1 && carryOut == 1){
carryOut = 1;
out = 0 | (0x1 >> i);
}else{
}//if else
}//for loop
printf("\n");
bit = (a >> 31) & 1;
bit2 = (a >> 31)& 1;
int bit3 = (out >> 31) & 1;
if( bit == 1 && bit2== 1 && bit3 == 0){
overflow = 1;
}else if (bit == 0 && bit2 == 0 && bit3 == 1){
overflow = 1;
}else{
}//overflow check
int j;
int g = 0;
for(j = 31; j>=0; j--){
if(g%4==0 && g!=0){
printf(" ");
}
bit2 = (out >> j) & 1;
printf("%d", bit2);
g++;
}
printf("\n");
}
int main (){
int a = 34;
int b = 17;
execute_add(a, b);
return 0;
}
With each of these statements in your for loop:
out = 0 | x;
You're resetting out, and clearing away all the work you've already done. You probably mean to do:
out = out | x
Or, equivalently,
out |= x
You also are right-shifting 1 all over the place, which is not what you're looking for; for any shift greater than zero, that's going to give you zero. I think you're often looking for a left-shift where you use a right shift.
I would second kaylum's comment about using a debugger; even if you flip the appropriate shifts and | with out properly, you're still going to have logic errors that will be easily fixed with a debugger.

What is this C function doing?

I understand that it's going to take in a number and return a character. I understand what each line of code is doing, but I can't really express what it's doing overall in a meaningful way. Any ideas?
int function(uint64_t P1) {
uint64_t L1 = P1;
uint32_t L2 = 1;
while (L1 > 15) {
L2 = (uint64_t)L2 << 4;
L1 = L1 >> 4;
}
uint32_t L3 = 0;
while (L2 != 0) {
L1 = P1;
uint32_t L4 = 0;
L4 = L1 % L2;
uint64_t L5 = (uint64_t)L4;
sub_function(L1 / L2);
L3++;
P1 = L5;
L2 = (uint64_t)L2 >> 4;
}
L1 = L3;
return L1;
}
void sub_function(uint64_t P1) {
if (P1 <= 9) {
printf("%c", P1 + 48);
} else {
printf("%c", P1 + 55);
}
}
This code accepts a decimal value and prints the hexadecimal representation.
Try it out yourself
In C we can use the %x (%X) format specifier flag to printf to do it for us:
printf("%X", 16); // 10
printf("\n");
printf("%X", 42); // 2A
If you're using a C++ compiler, we can instead use the std::hex stream manipulation flag on iostream to achieve the same result:
std::cout << std::hex << 16 << std::endl; // 10
std::cout << std::hex << 42 << std::endl; // 2a

Fixed point square root function wrong results for big numbers

Hello friends and enemies
I have this square root function from a library called libfixmath which works great, however from 32767.0f and above it starts returning wrong and negative results. The numbers I need square root of are rather big, up to 999999.0f. Any of you know what I could do to fix the problem?
#include <iostream>
float into_float(const int value) {
return ((float)value / 65536.0f);
}
int from_float(const float value) {
return (int)(value * 65536.0f);
}
int fp_sqrt(int value) {
unsigned char neg = (value < 0);
unsigned int num = (neg ? -value : value);
unsigned int result = 0;
unsigned int bit;
unsigned char n;
if (num & 0xFFF00000) {
bit = (unsigned int)1 << 30;
} else {
bit = (unsigned int)1 << 18;
}
while (bit > num) bit >>= 2;
for (n = 0; n < 2; n++) {
while (bit) {
if (num >= result + bit) {
num -= result + bit;
result = (result >> 1) + bit;
} else {
result = (result >> 1);
}
bit >>= 2;
}
if (n == 0) {
if (num > 65535) {
num -= result;
num = (num << 16) - 0x8000;
result = (result << 16) + 0x8000;
} else {
num <<= 16;
result <<= 16;
}
bit = 1 << 14;
}
}
if (num > result) {
result++;
}
return (neg ? -result : result);
}
void main() {
float flt_value = 32767.0f;
int int_value = from_float(flt_value);
float flt_root = sqrt(flt_value);
int int_root = fp_sqrt(int_value);
float flt_root2 = into_float(int_root);
printf("sqrt: %f fp_sqrt: %f", flt_root, flt_root2);
getchar();
}
Thank you leppie, I changed some stuff around and it works with big numbers now, I am not sure if what I did is totally right though but here it is:
#include <iostream>
float into_float(const int value) {
return ((float)value / 65536.0f);
}
long long from_float(const float value) {
return (long long)(value * 65536.0f);
}
long long fp_sqrt(long long value) {
unsigned char neg = (value < 0);
long long num = (neg ? -value : value);
long long result = 0;
long long bit;
unsigned char n;
if (num & 0xFFF00000) {
bit = (long long)1 << 60;
} else {
bit = (long long)1 << 36;
}
while (bit > num) bit >>= 2;
for (n = 0; n < 2; n++) {
while (bit) {
if (num >= result + bit) {
num -= result + bit;
result = (result >> 1) + bit;
} else {
result = (result >> 1);
}
bit >>= 2;
}
if (n == 0) {
if (num > 65535) {
num -= result;
num = (num << 16) - 0x8000;
result = (result << 16) + 0x8000;
} else {
num <<= 16;
result <<= 16;
}
bit = 1 << 14;
}
}
if (num > result) {
result++;
}
return (neg ? -result : result);
}
void main() {
float flt_value = 11932767.0f;
long long ll_value = from_float(flt_value);
float flt_root = sqrt(flt_value);
int int_root = (int)fp_sqrt(ll_value);
float flt_root2 = into_float(int_root);
printf("sqrt: %f fp_sqrt: %f", flt_root, flt_root2);
getchar();
}

Send a variable to a function and modify this variable in C

I have the following vars:
char seed[NBITS + 1], x0[NBITS + 1], y0[NBITS + 1], z0[NBITS + 1], dT0[NBITS + 1];
And i want to change it values on this function:
void lfsr(char *bin, char *output)
{
//bits significativos para fazer o xor NBITS -> NBITS,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (char)(48 + bit);
output = bin;
}
The way that I put the value in y0 from x is, for example, calling the lfsr functions like this:
lfsr(x0, y0);
What am I doing wrong?
I have to do 3 Fibonacci Linear Feedback Shift Register starting from x0.
x0 = 10101010101010
y0 = lfsr(101010101010)
z0 = lfsr(y0)
dT0 = lfsr(z0);
The results are good, but when I do the above code the value of x0 will be the same as dT0 if i use pointers.
Can anyone help me?
Thanks. Cumps!
Consider the following:
The numbers correspond to the taps. The bits are actually 15..0, left to right. The following is my implementation of the Fibonacci Linear Feedback Shift Register:
#include <stdio.h>
uint16_t fibLfsr(const uint16_t num)
{
uint16_t tempNum;
tempNum = (num) ^ (num >> 2) ^ (num >> 3) ^ (num >> 5);
tempNum = (tempNum & 0x1) << 15;
tempNum = (tempNum | (num >> 1));
return tempNum;
}
int main(void)
{
uint16_t testNum = 0xACE1;
printf("%#X\n", testNum);
testNum = fibLfsr(testNum);
printf("%#X\n", testNum);
return 0;
}
I'm not quite sure why you're using strings and converting them to binary. If this is necessary, you'll need some of the standard library APIs in stdlib and string to convert the string to an uint16_t before calling fibLfsr() and back to a string afterwards.

Resources