My function is returning this, error LNK2019: unresolved external symbol "double __cdecl MEAN - lnk2019

double mean(int A[], int size)
{
int count = size;
double sum = std::accumulate(A, A + count, 0 );
double ave = sum / count;
return ave;
}
I've included numeric but dont know what to do from here

Related

Best data type and rounding function for weight and currency variables

I need to multiply two values ​​- weight and currency (Visual c++, mfc). E.g.:
a=11.121;
b=12.11;
c=a*b;
Next I have to round "с" to 2 digits after point (currency value, e.g. 134.68). What the best data types and rounding function for this variables? The rounding procedure must be mathematically correct.
P.S. The problem was solved by very ugly but working part of code:
CString GetPriceSum(CString weight,CString price)
{
price.Replace(".", "");
price = price + "0";
if (weight.Find(".") == -1) { weight = weight + ".000"; }
weight.Replace(".", "");
unsigned long long int iprice = atoi(price);
unsigned long long int iweight = atoi(weight);
unsigned long long int isum = iprice * iweight;
CString sum = ""; sum.Format("%llu", isum);
CString r1 = sum.Right(1);
if (atoi(r1) >= 5) { isum += 10; }
CString r2 = sum.Mid(sum.GetLength() - 2, 1);
if (atoi(r2) >= 5) { isum += 100; sum.Format("%llu", isum);}
r2 = sum.Mid(sum.GetLength() - 3, 1);
if (atoi(r2) >= 5) { isum += 1000; sum.Format("%llu", isum);}
r2 = sum.Mid(sum.GetLength() - 4, 1);
if (atoi(r2) >= 5) { isum += 10000; sum.Format("%llu", isum);}
CString finsum = ""; finsum.Format("%llu", isum);
finsum.Insert(finsum.GetLength() - 6, ".");
finsum.Delete(finsum.GetLength() - 4, 4);
if (finsum.Left(1) == ".") { finsum = "0" + finsum; }
return finsum;
}
How about this: let's start from
API I use, counts values using some other language. And they round they values mathematically correct.
In your other question, you got those value as strings. You can construct an integer from those digits (remove decimal point). Assuming that the product fits in a 64-bit int, you can multiply them exactly. Now you can manually round to a desired precision and drop unneeded digits.
Code example (you may want to add error checking):
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::string a = "40.50";
std::string b = "0.490";
long long l1, dec1, l2, dec2;
sscanf(a.data(), "%lld.%lld", &l1, &dec1);
l1 = l1 * 100 + dec1;
sscanf(b.data(), "%lld.%lld", &l2, &dec2);
l2 = l2 * 1000 + dec2;
long long r = l1 * l2;
r /= 100;
int rem = r % 10;
r /= 10;
if (rem >= 5)
r++;
std::stringstream ss;
ss << r / 100 << "." << std::setw(2) << std::setfill('0') << r % 100;
std::cout << ss.str();
}
You can also use stringstream instead of sscanf to parse the strings.

Caesar problem code generating "error: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)'

I am doing the CS50 course and am on week 2. One of the problems of week 2 is called "Caesar". Essentially you have to write code which cyphers text by shifting letters that use the users inputted preferred number. After running my code I keep getting this error
"error: implicitly declaring library function 'strlen' with
type 'unsigned long (const char *)'
[-Werror,-Wimplicit-function-declaration]
for(i = 0, l = strlen(text); i < n; i++)"
This is the code:
int main(int argc, string argv[])
{
string n = argv[1];
int y = argc;
int key = get_int("./caesar ");//getting the number from the user
int k = (key);//assigning key a variable name.
string text = get_string("plaintext: ");//letting the user input their text.
if (key < 1)//Trying to make limit for acceptable input.
{
printf("ERROR");
return 1;
}
int l;
int i;
//for loop containing the encipher process
for(i = 0, l = strlen(text); i < n; i++)
{
if(isalpha(i))
{
if (isupper[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
else (islower[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
}
}
printf("ciphertext: %c", d || c);
return;
int checking_key(int y,string n)
int num = argc;
string key = y;
int num_key = atoi(key);
if(argc != 2)
{
return 0;
}
else
{
if (num_key > 0)
{
return num_key;
}
else
{
return 0;
}
}
}
From man strlen:
Synopsis
#include <string.h>
size_t strlen(const char *s);
Just like one needs to "include" cs50.h to use any of the get_* functions, string.h must be "include"d to access its functions, eg strlen.
Additionally (per comments):
The "ordered comparison" in the compile error
ordered comparison between pointer and integer ('int' and 'string' (aka 'char *')) [-Werror] for(i = 0, l = strlen(text); i < n; i++)
is i < n. Error says one of them is an int and one of them is a string.
On closer inspection this program is a long way from a clean compile. Recommend you follow along with the spec and "approach this problem one step at a time"

Finding the Krishnamurthy Number using C

I just want to know that for finding the Krishnamurthy number, we have to first find the factorial of the digits, then the addition of those numbers. (like, 1!+4!+5! = 145).
So, below is my code, and I have applied a factorial function over there. But the output is not coming in favor (145 is not a Kri...).
#include <stdio.h>
#include <stdlib.h>
void main()
{
int digit,factorial = 1, temp, input, sum = 0;
printf("Enter a Number:\n");
scanf("%d",&input);
int Factorial(int digit){
factorial = factorial*digit;
return 0;
}
temp = input;
while(temp>0){
digit = temp%10;
temp = temp/10;
sum = sum + Factorial(digit);
}
if(sum==input){
printf("%d is a Krishnamurthy Number",input);
}
else{
printf("%d is not a Krishnamurthy Number",input);
}
}
Have I done anything wrong in logic, or function declaration or definition? Please help.
your factorial function is not performing correctly. factorial means, multiplication of all digits starting from n downto 1 -
(n-1) * (n-2) * ... * (n)
but your function is not giving the desired result you want.
int Factorial(int digit){
factorial = factorial*digit;
return 0;
}
you need to change that function to get the factorial value of a number, you can either iterate a loop downto one or use a recursive approach to get the factorial.
int Factorial(int digit){
int result = 1;
for(int i=n; i>=1; i--){
result *= i;
}
return result;
}
or
int Factorial(int digit) {
if(n <= 1) return digit;
return digit * Factorial(digit-1);
}
you can follow the thread to understand the depth of recursive function mentioned above.
#include<stdio.h>
int main(int argc, char* argv[], char* envp[])
{
int sum = 0,
int a,
int p = 0,
int d,
int i,
int fact;
//code
printf("Enter a number: ");
scanf("%d", &a);
p = a;
while (a > 0)
{
fact = 1;
d = a % 10;
a /= 10;
for (i = d; i >= 1; i--)
{
fact *= i;
}
sum += fact;
}
if (sum == p)
printf("It is a Krishnamurthy number.\n");
else
printf("It is not a Krishnamurthy number.\n");
printf("\n\n");
return(0);
}

How to return a int converted to char array back to main for displaying it

My doubts are as follows :
1 : how to send 'str' from function 'fun' , So that i can display it in main function.
2 : And is the return type correct in the code ?
2 : the current code is displaying some different output.
char * fun(int *arr)
{
char *str[5];
int i;
for(i=0;i<5;i++)
{
char c[sizeof(int)] ;
sprintf(c,"%d",arr[i]);
str[i] = malloc(sizeof(c));
strcpy(str[i],c);
}
return str;
}
int main()
{
int arr[] = {2,1,3,4,5},i;
char *str = fun(arr);
for(i=0;i<5;i++)
{
printf("%c",str[i]);
}
return 0;
}
how to send 'str' from function 'fun' , So that i can display it in main function.
This is the way:
char* str = malloc( size );
if( str == NULL ) {
fprintf( stderr,"Failed to malloc\n");
}
/* Do stuff with str, use str[index],
* remember to free it in main*/
free(str);
And is the return type correct in the code ?
No, Probably char** is the one you need to return.
the current code is displaying some different output.
Consider explaining what/why do you want to do ? The way you have written, seems completely messed up way to me. You're passing array of integer but not its length. How is the fun() supposed to know length of array? Another problem is array of pointers in fun().
You can't write a int to a char (See the both size). So I used char array instead.
However, I'm not sure if this is what you want to do (might be a quick and dirty way of doing it):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char**
fun(int *arr, int size)
{
char **str = malloc( sizeof(char*)*size );
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
int i;
for(i=0;i<5;i++) {
str[i] = malloc(sizeof(int));
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
sprintf(str[i],"%d",arr[i]);
}
return str;
}
int
main()
{
int arr[] = {2,1,3,4,5},i;
char **str = fun(arr, 5);
for(i=0;i<5;i++) {
printf("%s\n",str[i]);
free(str[i]);
}
free(str);
return 0;
}
I made these changes to your code to get it working:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **fun(int *arr)
{
char **str = malloc(sizeof(char *) * 5);
int i;
for(i = 0; i < 5; i++) {
if ((arr[i] >= 0) && (arr[i] <= 9)) {
char c[2] ;
sprintf(c, "%d", arr[i]);
str[i] = (char *) malloc(strlen(c) + 1);
strcpy(str[i],c);
}
}
return str;
}
int main()
{
int arr[] = {2, 1, 3, 4, 5}, i;
char **str = fun(arr);
for(i = 0; i < 5; i++) {
printf("%s", str[i]);
free(str[i]);
}
printf("\n");
free(str);
return 0;
}
Output
21345
I added a check to make sure that arr[i] is a single digit number. Also, returning a pointer to a stack variable will result in undefined behavior, so I changed the code to allocate an array of strings. I don't check the return value of the malloc calls, which means this program could crash due to a NULL pointer reference.
This solution differs from the others in that it attempts to answer your question based on the intended use.
how to send 'str' from function 'fun' , So that i can display it in main function.
First, you need to define a function that returns a pointer to array.
char (*fun(int arr[]))[]
Allocating variable length strings doesn't buy you anything. The longest string you'll need for 64bit unsigned int is 20 digits. All you need is to allocate an array of 5 elements of 2 characters long each. You may adjust the length to suit your need. This sample assumes 1 digit and 1 null character. Note the allocation is done only once. You may choose to use the length of 21 (20 digits and 1 null).
For readability on which values here are related to the number of digits including the terminator, I'll define a macro that you can modify to suit your needs.
#define NUM_OF_DIGITS 3
You can then use this macro in the whole code.
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
Finally the receiving variable in main() can be declared and assigned the returned array.
char (*str)[NUM_OF_DIGITS] = fun(arr);
Your complete code should look like this:
Code
char (*fun(int arr[]))[]
{
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
int i;
for(i=0;i<5;i++)
{
snprintf(str[i],NUM_OF_DIGITS,"%d",arr[i]); //control and limit to single digit + null
}
return str;
}
int main()
{
int arr[] = {24,1,33,4,5},i;
char (*str)[NUM_OF_DIGITS] = fun(arr);
for(i=0;i<5;i++)
{
printf("%s",str[i]);
}
free(str);
return 0;
}
Output
2413345
With this method you only need to free the allocated memory once.

How to use a set of numbers as the Key for RCA Encryption

i would like to know how can i use a set of numbers as a KEY for the rc4 encryption.
According to the internet and wiki the KEY is actually a string of letters but the bytes are used . But in my program i need to use a 6 digit number as a KEY. Should i covert it to a string or how.
Key Sheudling Algorithm is indicated below.
void ksa(u_char *State, u_char *key) {
int byte, i, keylen, j=0;
keylen = (int) strlen((char *) key);
for(i=0; i<256; i++) {
j = (j + State[i] + key[i%keylen]) % 256;
swap(&State[i], &State[j]);
}
How can i modify the code or should i just convert the numbers to string.
Strings and numbers are both bytes. Here is a working RC4 code that accepts a key of unsigned chars:
#include<stdio.h>
#include<string.h>
#define SIZE 256
unsigned char SBox[SIZE];
int i;
int j;
void initRC4(unsigned char Key[]);
unsigned char getByte(void);
void initRC4(unsigned char Key[])
{
unsigned char tmp;
unsigned char KBox[SIZE];
for(i=0;i<SIZE;i++)
SBox[i]=i;
for(i=0;i<SIZE;i++)
KBox[i]=Key[i % strnlen(Key,SIZE)];
for(j=0,i=0;i<SIZE;i++)
{
j=(j+SBox[i]+KBox[i]) % SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
}
}
unsigned char getByte(void)
{
unsigned char tmp;
i=(i+1)%SIZE;
j=(j+SBox[i])%SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
return SBox[(SBox[i]+SBox[j])%SIZE];
}
First, you initialize the RC4 stream:
initRC4(key);
Then you do:
getByte()
...which always returns 1 byte from the RC4 stream you've set up.
One thing to remember though - a letter in string is not always equal to 1 byte. Same goes for the integers and number symbols in strings. Really, you must read an introduction to computer programming before you mess with ciphers.
Here is a demonstration of how bytes differ in strings in integers:
#include <string>
int main(int argc, char **argv) {
const int n=67898;
const std::string str = "67898";
const int arrayLength = sizeof(int);
const int stringArrayLength = str.size();
unsigned char *bytePtr=(unsigned char*)&n;
printf("Bytes for integer: ");
for(int i=0;i<arrayLength;i++)
{
printf("%X ", bytePtr[i]);
}
printf("\n");
printf("Bytes for string: ");
for(int i=0;i<stringArrayLength;i++)
{
printf("%X ", str.at(i));
}
printf("\n");
return 0;
}
Output:
Bytes for integer: 3A 9 1 0
Bytes for string: 36 37 38 39 38
There will usually be a terminating byte at the end of a string, so you could add +1 byte to string size.

Resources