I have a function for translating the fractional part of the entered number into another number system (not the most beautiful code):
void FracToAny(double D, int q, int t)
{
double b[10] = {0};
for (int i = 1; i < t; i++)
{
double DP = D * q;
D = modf(DP, &b[i]);
if (D == 0)
{
break;
}
D = DP - b[i];
}
for (int i = 0; i < t; i++)
{
if (q == 16)
{
switch ((int)b[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d", (int)b[i]);
break;
}
}
}
}
As a result, I get an array of double, which is a character-by-character representation of the resulting fractional part. However, I would like to return this value from the function in the form of "0.result". How can I do this?
It would be good not to use union or dynamic memory allocation (I've seen them trying to find a solution to the problem). I'd like to make it as simple as possible.
I think this is what you are trying to do (see my comment above):
Mac_3.2.57$cat etFract.c
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
double D;
int t = 3;
double b[10] = {0};
D = (3 * 16*16*16 + 4 * 16*16 + 5 *16)/(16*16*16.0);
printf("translating %f...\n", D);
for(i = 0; i < t; i++){
b[i] = modf(D, &D);
if(D == 0){
break;
}
D = b[i] * 16;
}
printf("0.");
for(int i = 0; i < t; i++){
switch((int)b[i]*16){
case 10:
printf("A\n");
break;
case 11:
printf("B\n");
break;
case 12:
printf("C\n");
break;
case 13:
printf("D\n");
break;
case 14:
printf("E\n");
break;
case 15:
printf("F\n");
break;
default:
printf("%d", (int)(b[i]*16));
break;
}
}
printf("\n");
return(0);
}
Mac_3.2.57$cc etFract.c
Mac_3.2.57$./a.out
translating 3.269531...
0.450
Mac_3.2.57$
Related
I am a new to C and I need to make a function that inserts a number to a 2D dynamic array depending on what is its last number in octal representation. I tried doing something like this.
Function generating an array:
void genArr(int m, int n, unsigned int** arr) {
arr = (unsigned int**)malloc(m * sizeof(unsigned int*));
for (int i = 0; i < m; i++) {
arr[i] = (unsigned int*)malloc(n * sizeof(unsigned int));
}
}
Function to insert numbers:
void insertToArr(unsigned int** arr) {
unsigned int num;
printf("Enter a number: \n");
for (int i = 0; i < 100; i++) {
scanf("%d", &num);
if (num != 0) {
switch (num % 8) {
case 0:
break;
case 1:
arr[1][i] = num;
break;
case 2:
arr[2][i] = num;
break;
case 3:
arr[3][i] = num;
break;
case 4:
arr[4][i] = num;
break;
case 5:
arr[5][i] = num;
break;
case 6:
arr[6][i] = num;
break;
case 7:
arr[7][i] = num;
break;
}
}
else if (num == 0) {
break;
}
}
}
But I keep getting exception error:
Unhandled exception at 0x00007FF630C019DE in App4.exe: 0xC0000005: Access violation reading location 0x0000000000000010.
I'm stuck and don't really know what is wrong.
This program is about converting Roman number to decimal number. The program can convert the alphabet to number but it can not process the last roman digit. I think my flow is alright but the output is not right. Can any body give me a helping hand?
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int roman_to_int(const char s[], int length) {
// Please complete the function body
int ans = 0, value[length];
for (int i = 0; i < length; i++) {
switch (s[i]) {
case 'I': value[i] = 1; break;
case 'V': value[i] = 5; break;
case 'X': value[i] = 10; break;
case 'L': value[i] = 50; break;
case 'C': value[i] = 100; break;
case 'D': value[i] = 500; break;
case 'M': value[i] = 1000; break;
}
}
for (int i = 0; i < length - 1; i++) {
if (value[i] >= value[i+1])
ans += value[i];
else {
ans = ans + value[i+1] - value[i];
i++;
}
}
return ans;
}
int main() {
char roman_num[] = "III";
char roman_num_2[] = "CXXIII";
char roman_num_3[] = "MMMCDLIX";
printf("roman_to_int(%s) = %d\n", roman_num,
roman_to_int(roman_num, strlen(roman_num)));
printf("roman_to_int(%s) = %d\n", roman_num_2,
roman_to_int(roman_num_2, strlen(roman_num_2)));
printf("roman_to_int(%s) = %d\n", roman_num_3,
roman_to_int(roman_num_3, strlen(roman_num_3)));
}
You should add the value of the last roman digit after the end of the second loop.
As an alternative, you could make value on entry longer than n and set the last entry to 0 so you won't need the make a special case of the last roman digit.
Note that you should also handle the case of unrecognised roman digits: either by ignoring them or by returning an error code, such as a negative value -1.
It is also simpler for roman_to_int to take a null terminated C string and compute the length there.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int roman_to_int(const char s[]) {
// Please complete the function body
int length = strlen(s);
int ans = 0, value[length + 1];
for (int i = 0; i < length; i++) {
switch (s[i]) {
case 'I': value[i] = 1; break;
case 'V': value[i] = 5; break;
case 'X': value[i] = 10; break;
case 'L': value[i] = 50; break;
case 'C': value[i] = 100; break;
case 'D': value[i] = 500; break;
case 'M': value[i] = 1000; break;
default: return -1;
}
}
value[length] = 0;
for (int i = 0; i < length; i++) {
if (value[i] >= value[i + 1])
ans += value[i];
else
ans -= value[i];
}
return ans;
}
int main() {
char roman_num[] = "III";
char roman_num_2[] = "CXXIII";
char roman_num_3[] = "MMMCDLIX";
char roman_num_4[] = "MMMCDLIZ"; // error
printf("roman_to_int(%s) = %d\n", roman_num, roman_to_int(roman_num));
printf("roman_to_int(%s) = %d\n", roman_num_2, roman_to_int(roman_num_2));
printf("roman_to_int(%s) = %d\n", roman_num_3, roman_to_int(roman_num_3));
printf("roman_to_int(%s) = %d\n", roman_num_4, roman_to_int(roman_num_4));
return 0;
}
I am making a game for school this is a basic slot machine it will randomly generate numbers and converts the numbers to chars in a separate array. This seems to not be working as the statement is completely ignored. It doesn't give the right output and some time they will just go to blanks.
Output:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*
George Mason
Slots
Date Started: 4/25/16
Date Finished:
Dr K.
*/
void loadScreen();
void spacer();
void tabSpacer();
void clearScr();
int printMachine(char*, int);
int randomNum(int*, int);
int convertNum(int*, char*, int);
int rand_int();
void game();
int main(){
srand(time(NULL));
int stop;
loadScreen();
clearScr();
game();
scanf("%d", &stop);
}
void game(){
int tokens = 5, randomNums[9], x, y = 9;
char randomChars[9], userInput;
randomNum(randomNums, 9);
convertNum(randomNums, randomChars, 9);
printMachine(randomChars, 9);
}
int printMachine(char* randomChars, int y){
printf("-------------\n");
printf("| %c | %c | %c | \n", randomChars[0], randomChars[1], randomChars[2]);
printf("| %c | %c | %c | \n", randomChars[3], randomChars[4], randomChars[5]);
printf("| %c | %c | %c | \n", randomChars[6], randomChars[7], randomChars[8]);
printf("-------------");
}
int randomNum(int* randomNums, int y){
int x,a = 0, b = 9;
for(x = 0; x < y; x++){
randomNums[x] = ((rand() % (b-a+1)) + a);
}
}
int convertNum(int* randomNums, char* randomChars, int y){
int x;
for(x = 0; x < 9; x++){
switch(randomNums[x]){
case 1:
randomChars[x] = '#';
break;
case 2:
randomChars[x] = '#';
break;
case 3:
randomChars[x] = '$';
break;
case 4:
randomChars[x] = '+';
break;
case 5:
randomChars[x] = '&';
break;
case 6:
randomChars[x] = '*';
break;
case 7:
randomChars[x] = '?';
break;
case 8:
randomChars[x] = '!';
break;
case 9:
randomChars[x] = '~';
break;
default:
randomChars[x] = 'e';
break;
}
}
}
void loadScreen(){
int x;
for(x = 0; x < 3; x++){
spacer();
}
tabSpacer();
printf("Please wait 5 seconds while we load the saved data.\n");
tabSpacer();
printf(" If there is no saved data one will be created.");
sleep(5);
}
void spacer(){
int x;
for(x = 0; x < 3; x++){
printf("\n");
}
}
void tabSpacer(){
printf("\t ");
}
void clearScr(){
system("cls");
}
You have forgot to keep break; after every case so it will run all cases i.e from case 1 to case 9.
At the end it will save randomChars[x] = '~'; (case 9).
case 1:
randomChars[x] = '#';
break;
case 2:
randomChars[x] = '#';
break;
.
.
.
.
case 7:
randomChars[x] = '?';
break;
case 8:
randomChars[x] = '!';
break;
case 9:
randomChars[x] = '~';
break;
EDIT:
why your function return type is int when you are not returning any value. change int to void.
function game() is not declared before main()
I am getting Output like this:
I have a task to print a list of words made up of "non-vowel, vowel, non-vowel", i.e bab, bac, bad, bad ... through to zuz.
I have managed to create a code which does the first two letters but gets lost on the last loop and prints only '}' - which seems strange to me. The code is below:
#include <stdio.h>
#include <string.h>
int check_vowel(char c);
int check_consonant(char c);
int main ()
{
char c, c2, c3;
int cnt;
for (cnt = 0; cnt <= c; cnt++)
{
for (c = 'a'; c <= 'z'; c++)
{
if (check_vowel(c) == 0)
{
for (c2 = 'a'; c2 <= 'z'; c2++)
{
if (check_consonant(c2) == 0)
{
for (c3 = 'a'; c3 <= 'z'; c3++);
{
if (check_vowel(c3) == 0)
{
cnt++;
printf("%d || %c%c%c\n", cnt, c, c2, c3);
}
}
}
}
}
}
}
printf("Total names = %d", cnt);
return 0;
}
int check_vowel(char c)
{
switch(c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return 1;
default:
return 0;
}
}
int check_consonant(char c)
{
switch(c)
{
case 'b':
case 'c':
case 'd':
case 'f':
case 'g':
case 'h':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
return 1;
default:
return 0;
}
}
The outputs are along the lines of:
1 || ba}
2 || be}
etc
The first bug is in this line:
for (c3 = 'a'; c3 <= 'z'; c3++);
Because of the semicolon at the end of that line, the iteration body is "do nothing". The block of code after it (from { to }) is just treated as something to do after this loop. When you enter this block of code, c3 will always be {, because it's the first character after z (in most character encodings). The fix: remove the semicolon.
The second bug is in the choice of conditions. The condition check_vowel(c3) == 0 (what you wrote) is not equal to check_consonant(c3) == 1 (what you want). For instance, { is not a vowel, but that doesn't make it a consonant. The fix: make your conditions positive.
Why don't try to iterate only over the proper sets:
static const char cons[] = "bcdfghjklmnpqrstvwxyz";
static const size_t cons_sz = sizeof cons - 1;
static const char vowels[] = "aeiou";
static const size_t vowels_sz = sizeof vowels - 1;
for (i = 0; i < cons_sz; i++)
for (j = 0; j < vowels_sz; j++)
for (k = 0; k < cons_sz; k++)
printf("%c%c%c\n", cons[i], vowels[j], cons[k]);
This question already has answers here:
Undefined reference to `sin` [duplicate]
(4 answers)
Closed 9 years ago.
../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c
Building target: NoteConverter
Invoking: GCC C Linker
gcc -o "NoteConverter" ./NoteConverter.o
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1
The following is code
/**
* Frequency octave finder and play note
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#define DURATION 5
#define SAMPLERATE 48000
#define AMPLITUDE 1
//A*sin(2*pi*n*f/R)
double yCord(int n, double freq)
{
double y= sin(2*M_PI*freq*n/48000);
return y;
}
int xCord(int n)
{
double x = n/SAMPLERATE;
return x;
}
// finds the freq from switch
int noteBaseFeq(char *note) {
int baseNote = 0;
int baseFreq = 0;
const char *notes[] = { "C ", "C#", "Db", "D ", "D#", "Eb", "E ",
"F ", "F#", "Gb", "G ", "G#", "Ab", "A ", "A#", "Bb", "B " };
int i;
for( i = 0; i < 17; i++) {
if (strcmp(note,notes[i]) == 0) {
baseNote = i;
break;
}
}
switch (baseNote+1) {
case 1: // c
baseFreq = 26163;
break;
case 2: // c#
case 3: // Db
baseFreq = 27718;
break;
case 4: // D
baseFreq = 29366;
break;
case 5: //D#
case 6: //Eb
baseFreq = 31113;
break;
case 7: //E
baseFreq = 32963;
break;
case 8: //F
baseFreq = 34923;
break;
case 9: //F#
case 10: //Gb
baseFreq = 36999;
break;
case 11: //G
baseFreq = 39200;
break;
case 12: //G#
case 13: //Ab
baseFreq = 41530;
break;
case 14: //A
baseFreq = 44000;
break;
case 15: //A#
case 16: //Bb
baseFreq = 46616;
break;
case 17: //B
baseFreq = 49388;
break;
}
return baseFreq;
}
// finds the octive based on A 4
int octaveModifier(int oct)
{
if (oct == 4) {
return oct;
} else if (oct < 4) {
oct = 4 - oct;
} else if (oct > 4) {
oct = oct - 4;
}
return oct;
}
// allows for concatenation
void append(char* s, char c)
{
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int main()
{
char note;
char modifier;
char inputNote[256] = "";
long long intFreq = 0;
double freq = 0.0;
int oct = 0;
int modOct = 0;
printf("Please enter an note sharp\\flat(\"b\" for flat) and octave: \n");
scanf("%c%c%d", ¬e, &modifier, &modOct);// gets input from user
append(inputNote, note);
append(inputNote, modifier);
oct = octaveModifier(modOct);
intFreq = noteBaseFeq(inputNote);
int i;
if(modOct > 4) {
for(i = 0; i < oct; i++) {
intFreq = intFreq * 2;
freq = intFreq / 100.0;
}
} else if(modOct < 4) {
for( i = 0; i < oct; i++) {
intFreq = intFreq / 2;
freq = intFreq / 100.0;
}
} else {
freq = intFreq / 100.0;
}
double time = 0;
double position = 0;
for (i = 0; time < DURATION; i++) {
time = xCord(i);
position = yCord(i, freq);
}
return 0;
}
You probably need to link with -lm to get the maths library linked. Some platforms require that; others do not.
Fixing the set-but-unused variable is easy; remove its declaration and the line where it is set (or use the result — print it, perhaps?).