How can I write the total value at the beginning - c

#include <stdio.h>
int main() {
int k;
unsigned long long int aray[94];
aray[0]=0;
aray[1]=1;
unsigned long long int total=0;
printf("\n FIBONACCI : \n\n");
printf("1 + ");
for(k=2;k<=93;k++){
aray[k]=aray[k-1]+aray[k-2];
total+=aray[k];
printf("%llu + ",aray[k]);
}
return 0;
}
Hi guys I need to print total value printf("%llu",total);at the begining but I don't know how to do this thanks

calculate and "print" into a large enough string (or realloc() as needed)
char bigenough[200000];
char *p = bigenough;
loop {
/* ... calculate ... */
p += sprintf(p, "%llu + ", value);
}
afterwards print total and result string
printf("total: %llu = ", total);
printf("%s\n", bigenough);

Related

Juxtaposing integer ASCII codes for alphabet

I want to convert Name and Surname(e.g., Nova Stark) into a large integer by juxtaposing integer ASCII codes for alphabet, print the corresponding converted integer then cut the large integer into two halves and add the two halves.Following is my approach:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* arr2str(int arr[], int size) {
static char buffer[256];
memset(&buffer[0], 0, sizeof(buffer)/sizeof(char));
char *ptr = &buffer[0];
for(int i=0; i<size; ++i) {
sprintf(ptr += strlen(ptr), "%d", arr[i]);
}
return buffer;
}
int arr2int(int arr[], int size)
{
char buffer[256] = {0,};
char *ptr = &buffer[0];
for(int i = 0; i < size; ++i) {
sprintf(ptr += strlen(ptr), "%d", arr[i]);
}
return atoi(&buffer[0]);
}
int main()
{
int *A;
long long int num;
int div,base=10;
char name[50],asc[200];
printf("Enter your name : ");
scanf(" %[^\n]",name);
int len=strlen(name);
A=(int*)malloc(len*sizeof(int));
for(int i=0;i<len;i++)
{
A[i]=name[i];
}
char *str = arr2str(A, len); //for converting array to string
num = arr2int(A, len); //again for converting the character array to integer.
//num=array_to_num(A,len);
div=base;
while(num/div>div)
{
div=div*base;
}
long long int a=num/div;
long long int b=num%div;
long long int c=a+b;
printf("The required integer is %lld and the sum is %lld ",num,c);
return 0;
}
But I am not getting the desired output. Please help!
Also, if there exists a simpler approach to the problem please specify that too.
Following is running, for explanations see comments in the code. Maybe you have to handle the whitespaces in the name:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *A;
long long int num;
int div,base=10;
char name[50];
printf("Enter your name : ");
scanf(" %[^\n]",name);
int len=strlen(name);
A=(int*)malloc(len*sizeof(int));
for(int i=0;i<len;i++)
{
*(A+i)=(int)name[i]; // *(A+i) is accessing array as pointer+index : https://www.programiz.com/c-programming/c-dynamic-memory-allocation
// letters to ascii is done by simply typecasting to (int)
}
// string to int array
for(int i=0;i<len;i++)
{
printf("%i -> %c \n", *(A+i), (char)(*(A+i)));
}
// long long int from concatenating the elements of the int array
int s_idx=0; // index for string
char str[512]; //string of fixed size, possibly malloc this
for (int i=0; i<len; i++)
s_idx += snprintf(&str[s_idx], 512-s_idx, "%d", *(A+i));
printf("%s \n", str);
num = strtoll(str, NULL, 10); // https://en.cppreference.com/w/c/string/byte/strtol
// from here your code is unchanged
div=base;
while(num/div>div)
{
div=div*base;
}
long long int a=num/div;
long long int b=num%div;
long long int c=a+b;
printf("The required integer is %lld and the sum is %lld \n ",num,c);
free(A);
return 0;
}

In C: How to print function argument

I want to print function argument in the printf command. Please help to suggest.
void printline(char ch, int len);
value(float, float, int);
main()
{
double amount;
printline('=', 30);
amount = value(500, 0.12, 5); // I want to print argument of function value. please help
printf("The total amount is: %f \n", amount);
//printf("%f\t%f\t%d\t%f \n", 500, 0.12, 5, amount);
printline('=', 30);
_getch();
}
void printline(char ch, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c", ch);
printf("\n");
}
value(float p, float r, int n)
{
int year;
float sum;
sum = p;
year = 1;
while (year <= 5)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}
In your printline function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string or array of characters you want to use char * or char[] and iterate through it. So your function printline could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("\n");
}
just make sure that len isn't bigger then the length of *ch.
Even better solution, where you don't have to worry about the value of len is to print the characters one by one until you come across the \0 character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != '\0'; ++i)
printf("%c", ch[i]);
printf("\n");
}
Or even better
If the string is null terminated, use printf("%s", ch). If the string is not null terminated but the length is supplied, use printf("%.*s", len, ch). In both cases, there's no loop in the user code; the loop is buried inside the printf() function. Further, since there's a newline printed after the loops, use printf("%s\n", ch) or printf("%.*s\n", len, ch) and skip the extra printf() after the loop.
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)\n", __func__, f, r, n);

How to replace a character at random in a string?

I've printed a string of "+" symbols based on two given values(N, M). Now I'm trying to figure out how to replace characters at random in said string based on a third given value(K). The characters are stored in a string(l). I think I have to use the replace function but I don't know how(hence why it's in a comment for now). Any help is appreciated.
#include <stdio.h>
unsigned int randaux()
{
static long seed=1;
return(((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s[1000];
int N, M, K, l;
printf("N: ");
scanf("%d",&N);
printf("M: ");
scanf("%d",&M);
printf("K: ");
scanf("%d",&K);
printf("\n");
gets(s);
l=strlen(s);
/* Mostre um tabuleiro de N linhas e M colunas */
if(N*M<K){
printf("Not enough room.");
}else if(N>40){
printf("Min size 1, max size 40.");
}else if(M>40){
printf("Min size 1, max size 40.");
}else{
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
printf("+", s[j]);
}
printf("\n", s[i]);
}
for(int l=0; l<K; l++)
{
/*s.replace();*/
}
}
return 0;
}
There is too much unexplained complexity and unknowns in your program to enable a corrective answer. But this shows how to replace a textual string's character at random, with a numeral.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
char str[] = "----------";
int len = strlen(str);
int index;
int num;
srand((unsigned)time(NULL)); // randomise once only in the program
printf("%s\n", str); // original string
index = rand() % len; // get random index to replace, in length range
num = '0' + rand() % 10; // get random number, in decimal digit range
str[index] = num; // overwrite string character
printf("%s\n", str); // altered string
return 0;
}
Program sessions:
----------
-3--------
----------
-----0----
----------
--------6-
Arguably it would be better to use size_t types, but for the limited range of the example, will suffice.

How to use corecursion in c?

I need help trying to find where to place the print statements in each function (alpha_count and sum_digits) so that they will only print once (at the end of the program).
Ex.
Number of characters: 8
Sum of digits: 19
As of right now they print each time the function has been called. Any ideas?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//Prototypes
void count_alpha(char *s, int len, int index);
void sum_digit(char *s, int len, int index);
#define SIZE 22
int main(void){
//Declarations
char string[SIZE];
int length;
//Get string from user
printf("Enter a string of letters and numbers: ");
scanf("%s", string);
printf("String: %s\n", string);
//Get length of string
length = strlen(string);
printf("Length: %d\n", length);
//Get letters from string
count_alpha(string, length, 0);
return 0;
}
void count_alpha(char *s, int len, int index){
static int characters = 0;
char c = ' ';
if (index < len){
c = s[index];
if(isalpha(c)){
characters++;
printf("char: %d\n", characters);
index++;
printf("index: %d\n", index);
count_alpha(s, len, index);
}
else if(isdigit(c)){
sum_digit(s, len, index);
}
//index++;
//printf("index: %d\n", index);
//printf("Number of Characters: %d\n", characters);
}
//else
printf("Number of Characters: %d\n", characters);
}
void sum_digit(char *s, int len, int index){
static int digits = 0;
char c = ' ';
if (index < len){
c = s[index];
if(isalpha(c)){
count_alpha(s, len, index);
}
else if(isdigit(c)){
printf("num is: %c", c);
//printf("number is: %d", (atoi(&s[index])));
//digits += atoi(&c);
digits += c - '0';
printf("sum: %d\n", digits);
index++;
printf("index: %d\n", index);
sum_digit(s, len, index);
}
//index++;
//printf("index: %d\n", index);
//printf("Sum of digits: %d\n", digits);
}
//else
printf("Sum of digits: %d\n", digits);
}
Declare int characters = 0 and int digits =0 globally, keeping them global will be of same use as of static variables in addition it can be accessed anywhere thus will help you in printing what you wanted just once in main function.
For declaring them global just declare them outside all functions and at start of program.
.
.//header files
.
//#include <ctype.h>
int characters = 0;
int digits = 0;
In main(): just print them
printf("Number of Characters: %d\n", characters);
printf("Sum of digits: %d\n", digits);
EDIT: Without global variables by using pointers.
void count_alpha(char *s, int len, int index,int *charac,int *dig);
void sum_digit(char *s, int len, int index,int *charac,int *dig);
In main:
int* charac=&characters;
int* dig=&digits;
count_alpha(string,length,0,charac,dig);
And whenever you see a function call pass these pointers:
count_alpha(string,length,0,charac,dig);
For incrementing values just use:
(*charac)++;
(*dig)++;
As alpha_count() and sum_digits() are effectively doing some statistics gathering, pass in a pointer to a statistics type. Drop the static variables.
typedef struct {
unsigned characters;
unsigned digits;
} char_digs;
void sum_digit(char *s, int len, int index, char_digs *stat) {
// static int digits = 0;
...
count_alpha(s, len, index, stat);
...
stat->digits += c - '0';
...
sum_digit(s, len, index, stat);
...
}
void count_alpha(char *s, int len, int index, char_digs *stat) {
// like sum_digit() above
}
int main(void) {
...
char_digs stat = {0,0};
...
count_alpha(..., ..., &stat);
printf("Number of Characters: %u\n", stat.characters);
printf("Sum of digits: %u\n", stat.digits);
}

two-digit string addition with no number at the end

I have to add two digit strings, meaning 1234 12+34 (at least that's what i gather). I wrote a program that does this expect for one exception, that is when the last number doesn't have a pair it wont add properly.
Here is the code i have:
void main()
{
char string[1000];
int count,sum=0,x,y;
printf("Enter the string containing both digits and alphabet\n");
scanf("%s",string);
for(count=0;count < string[count]; count++)
{
x=(string[count] - '0') * 10;
y=(string[count+1] - '0') + x;
sum += y;
count++;
}
printf("Sum of string in two digit array is =%d\n",sum);
}
so basically if i have 123 the program does 12+(30-48), instead of 12+3. Ive been sitting on it for a while, and cant figure out how to fix that issue, any tips or advice would be welcomed.
(Strings like 1234 or 4567 will do 12+34 and 45+67)
#include <stdio.h>
#include <ctype.h>
int main(void){
char string[1000];
char digits[3] = {0};
int i, j, x, sum = 0;
printf("Enter the string containing both digits and alphabet\n");
scanf("%999s", string);
for(j = i = 0; string[i]; ++i){
if(isdigit(string[i])){
digits[j++] = string[i];
if(j==2){
sscanf(digits, "%d", &x);
sum += x;
j = 0;
}
}
}
if(j==1){
digits[j] = 0;
sscanf(digits, "%d", &x);
sum += x;
}
printf("Sum of string in two digit array is = %d\n", sum);
return 0;
}

Resources