Count Unique Integers, and Large Integer Math in C

Count Unique Integers, and Large Integer Math in C Homework Sample

Create a program count2.c which counts the number of occurrences of each value (using a linked list, rather than an array). Create another program strmath.c that allows for huge numbers (bigger than 32 or 64 bit) by treating them as strings. The first line of the file should be either add or sub, and then the next 2 lines should be integer values. For more C programming assignments contact us for details.

Solution:

count2.c

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
void sort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (arr[i] > arr[j])
{
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
}
}
bool isNotDigit(char c)
{
return (c>= ‘0’ && c <=’9′)==0;
}
bool has_no_digit (char * in)
{
int len = strlen(in);
for(int i = 0; i < len; i++)
{
if(isNotDigit(in[i]) && in[i] != ‘ ‘&& in[i]!= ‘\n’)
{
if (in[i] == ‘-‘ && i <len-1)
{
continue;
}
return 1;
}
}
return 0;
}
typedef struct node
{
int val;
struct node *next;
} node_t;

/*
function that insert in the sorted linked list
*/
void insert(node_t **root, int data)
{
node_t *temp, *tmp;
if (*root == NULL)
{
/*List is empty*/
*root = malloc(sizeof(node_t)); /*malloc the root */
if(*root == NULL)
{
puts(“ERROR MALLOC()”);
exit(1);
}
(*root)->next = NULL; /*mark the next as null*/
(*root)->val = data; /*saving the data to the value */
}
else
{
/*list is not empty */
if ((*root)->val > data)
{
/* the given token is equal to or larger than the root token*/
/*push the given token to the beging*/
temp = malloc(sizeof(node_t));
if (temp == NULL)
{
puts(“ERROR MALLOC()”);
exit(1);
}
temp->val = data;
temp->next = *root;
*root = temp;
}
else
{
temp = *root;
while (1)
{
if (temp->next == NULL)
{ /* this means that the given token is smaller than every thing in the list
add it to the end
*/

temp->next = malloc(sizeof(node_t));
if (temp == NULL)
{
puts(“ERROR MALLOC()”);
exit(1);
}
temp->next->val = data;
return;
}
if ( temp->next->val > data)
{
/*
the given token is larger or equal to some string in the middle
1-break the connection
2-add a node in the middle
3-connect it to the next node
*/
tmp = malloc(sizeof(node_t));
if (tmp == NULL)
{
puts(“ERROR MALLOC()”);
exit(1);
}
tmp->val = data;
tmp->next = temp->next;
temp->next = tmp;
return;
}
temp = temp->next;
}
}
}
}

int main()
{
int *cnt_pos, *cnt_neg, temp, cnt = 0;
cnt_neg = (int *)malloc(10000000); //counter arrays for negative numbers
cnt_pos = (int *)malloc(10000000); //counter arrays for positive numbers
node_t* arr = NULL;
if (cnt_neg == NULL || cnt_pos == NULL)
{
puts(“ERROR MALLOC()”);
exit(1);
}
char in[100];
while(scanf(“%s”, in) == 1)
{
if(has_no_digit(in)){
puts(“ERROR: NON integer input”);
exit(1);
}
sscanf(in ,”%d”, &temp );

temp %= 10000000;
insert(&arr, temp);
if (temp < 0)
{
temp *= -1;
cnt_neg[temp]++;
}
else
{
cnt_pos[temp]++;
}
}

node_t *tmp = arr;
while(tmp!=NULL)
{

int holder = tmp->val;
tmp = tmp->next;
if (holder < 0)
{
holder *= -1;
if (cnt_neg[holder])
{
printf(“%d %d\n”, -holder, cnt_neg[holder]);
cnt_neg[holder] = 0;
}
}
else
{
if (cnt_pos[holder])
{
printf(“%d %d\n”, holder, cnt_pos[holder]);
cnt_pos[holder] = 0;
}
}
}
free(cnt_neg);
free(cnt_pos);
free(arr);

return 0;
}

strmath.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

void reverse(char *str)
{
int len = strlen(str);
char c ;
for(int i = 0; i < len/2; i++)
{
c = str[i];

str[i] = str[ (unsigned int) (len -i-1) ];
str[(unsigned int)(len – i – 1)] = c;
}
}
char* getSum(char* str1, char* str2)
{

if (strlen (str1) > strlen(str2))
{
char* tmp = str1;
str1 = str2;
str2 = tmp;
}

char* str = (char *) malloc(1);
if(str == NULL)
exit(1);
int n1 = strlen(str1), n2 = strlen(str2);
int diff = n2 – n1;

int carry = 0;
int newsz = 1;
for (int i = n1 – 1; i >= 0; i–)
{
int sum = ((str1[i] – ‘0’) +
(str2[i + diff] – ‘0’) +
carry);
str[newsz-1]=(sum % 10 + ‘0’);

str = (char *)realloc(str, newsz + 1);
newsz++;
carry = sum / 10;
}

for (int i = n2 – n1 – 1; i >= 0; i–)
{
int sum = ((str2[i] – ‘0’) + carry);
str[newsz – 1] = (sum % 10 + ‘0’);
str = (char *)realloc(str, newsz + 1);
newsz++;
carry = sum / 10;
}

if (carry)
{
str[newsz-1]=(carry + ‘0’);
str = (char *)realloc(str, newsz + 1);
newsz++;
}
reverse(str);
return str;
}

bool isSmaller(char* str1, char* str2)
{
// Calculate lengths of both char*
int n1 = strlen(str1), n2 =strlen (str2);

if (n1 < n2)
return true;
if (n2 > n1)
return false;

for (int i = 0; i < n1; i++)
{
if (str1[i] < str2[i])
return true;
else if (str1[i] > str2[i])
return false;
}
return false;
}

char* getSub(char* str1, char* str2)
{

int smaller = 0;
if (isSmaller(str1, str2))
{
char * t = str1;
str1 = str2;
str2 = t ;
smaller = 1;
}
int newsz = 1;
char *str;
str = (char*)malloc(1) ;
if(str == NULL)
{
exit(1);
}
int n1 = strlen(str1), n2 =strlen( str2);
int diff = n1 – n2;

int carry = 0;

for (int i = n2 – 1; i >= 0; i–)
{

int sub = ((str1[i + diff] – ‘0’) –
(str2[i] – ‘0’) –
carry);
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str[newsz – 1] = (sub + ‘0’);

str = (char *)realloc(str, newsz + 1);
newsz++;

}

for (int i = n1 – n2 – 1; i >= 0; i–)
{
if (str1[i] == ‘0’ && carry)
{
str[newsz – 1] = ( ‘9’);

str = (char *)realloc(str, newsz + 1);
newsz++;
continue;
}
int sub = ((str1[i] – ‘0’) – carry);
if (i > 0 || sub > 0) // remove preceding 0’s
{
str[newsz – 1] = (sub + ‘0’);

str = (char *)realloc(str, newsz + 1);
newsz++;
}
carry = 0;
}

// reverse resultant char*
int len = strlen(str);
len–;
while(len>0 && str[len]==’0′ )
{
str[len] = ‘\0’;
len–;
}
if(smaller)
str[len+1] = ‘-‘;
reverse(str);

return str;
}
void check(char *c)
{
char a []= “add”, b[] =”sub”;
//printf(“%s”, c);
if(strcmp(c ,a) != 0 && strcmp(c , b)!=0)
{
puts(“Error: 1st line not equal to ‘add’ or ‘sub'”);
exit(1);
}
}
void run(char * a, char*b, char*c)
{
char* ans;
if(strcmp(c ,”add”) == 0)
{
ans = getSum(a, b);
}
else
{
ans = getSub(a,b);
}
printf(“%s\n”, ans);
}
int main()
{

char * a , *b , c[5];
scanf(“%s”, c);
check(c);
int asz = 0 , bsz=0;
a= (char *) malloc(1);
b= (char *) malloc(1);
if(a==NULL || b == NULL)
{
exit(1);
}
char temp;
scanf(“%c”, &temp);
while(scanf(“%c”, &temp) && temp != ‘\n’)
{
a[asz] = temp;
asz++;
a = (char *) realloc(a,asz+1);

}
while(scanf(“%c”, &temp) && temp != ‘\n’)
{
b[bsz] = temp;
bsz++;
b = (char *) realloc(b,bsz+1);

}
run(a, b ,c);
free(a);
free(b);
return 0;
}