Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Good night, what's the best way in C to count the number of possibilities of UNIQUE anagrams in a string with the maximum length of 256 without allows repetitions caused by same letters? The input would be just in uppercase and only alphabet letters are allowed, A-Z. I got stuck in the worst case of the program that got 26! a very large number that overflow even my double. I think I'm very lost here, I'm not so good in C. The program just need to shows the number of possibilities, not the anagram. Like:
LOL = 3
HOUSE = 120
OLD = 6
ABCDEFGHIJKLMNOPQRSTUVWXYZ = 403291461126605635584000000
Thank you guys very much... I tried a lot and failed in every single tried, I'm in distress with it. The way I got more closer of do it was in Pascal, but it also failed in some tests, and I can't use Pascal anyway. I'm using CodeBlocks on Windows that compiles with GCC.
You should calculate factorial of the length of the given string divided by the factorial of the occurrence of every letter.
long double logFactorial (int i) {
return i < 2 ? 0.L : (logFactorial (i-1)+log(long double (i));
}
int countLetter(const char* str, char c) {
int res = 0;
while (str && *str) {
res += *str++ == c;
}
return res;
}
long double numPermutations(const char* str) {
auto res = logFactorial (strlen(str));
for (char c = 'A'; c<='Z'; c++) {
res -= logFactorial (countLetter (str,c));
}
return exp((long double)res);
}
Pay attention!
There are several people here who were correct by saying that factorial of 26 cannot be stored even in 64bit integer.
Therefore, I changed my calculation to the logarithmic of the factorial number and store it in long double which I hope is precise enough (I think that the exp() function is not precise enough)
Nevertheless you cannot use this result as an integer value, unless you find a way to store it in 128bit integer or bigger...
You should test it too if this fits your problem.
There is a faster way to calculate the factorial for this problem by storing the results of 0! up to 26! in an array of the size of [27].
I will leave it to you for asking another question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My teacher gave us a question in programming class today and I don't understand how he got the answer. I was hoping someone could explain it to me. We basically have to show what the programs output would be, however I am somewhat confused as to how to get the answer to the question. The Question is as follows:
#include <stdio.h>
void do_something (int , int * );
int main (void)
{
int first = 1, second = 2 ;
do_something(second, &first);
printf("%4d%4d\n", first, second);
return (0);
}
void do_something (int thisp, int *that)
{
int the_other;
the_other = 5;
thisp = 2 + the_other;
*that = the_other * thisp;
return;
}
Answer
35 and 2
The function do_something contains 2 parameters.
normal integer (thisp)
pointer to an integer. (that)
What your teacher wanted you to learn is, pass by value and pass by address.
In pass by value, the original value doesn't change. This is because in the example given.
value of variable second is copied thisp variable.
In pass by address, the original value can be modified within the function.
This is because, the pointer that is pointing to the location of variable first. So if value of that is changed, the value of first will also change.
This is why, value of first is changed in the output and value of second is unaffected.
thisp = 2 + the_other;
*that = the_other * thisp;
Means:
thisp = 2 + 5
*that = 5 * 7
And that contains address of first in main which is overwritten in do_something as 35. Second remains 2.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to prompt the user to enter a number, and then have the computer create that many arrays.
For example if the user entered the number 5, I would would want 5 integer arrays called array1[64], array2[64], array3[64], array4[64], and array5[64] to be created.
You can use array of pointers
Like following :-
int n,i;
//enter n
int **array = malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
array[i] = malloc(sizeof(int)*64);
/* Do Stuffs*/
/* Free Memory */
for(i=0;i<n;i++)
free(array[i]);
free(array);
Anytime you say, "I want N variables of the same type named var1, var2, var3, ..., varN", what you really want is an array; in this case, you want an array of arrays.
Assuming that you know the second dimension at compile time (i.e., it's always going to be an Nx64-element array of int), then this is easy:
#include <stdlib.h>
...
size_t numArrs = 0;
// get numArrs from user
int (*arrs)[64] = malloc( sizeof *arrs * numArrs );
Presto - you've allocated an Nx64 array of int that you can access like any normal 2D array:
arrs[i][j] = some_value();
arrs[0] is your first 64-element array of int, arrs[1] is your second 64-element array of int, etc.