Question

In: Computer Science

can you convert this cod from C++ to C language #include<bits/stdc++.h> using namespace std; // Function...

can you convert this cod from C++ to C language

#include<bits/stdc++.h>
using namespace std;
// Function for finding sum of two large numbers
string addThese(string a,string b){
  
// Before proceeding further, make sure length
// of a is larger.
if(a.size() < b.size())
swap(a,b);

int j = a.size() - 1;
// Traverse from end of both strings and add (b[i] - '0') to a[j].
for(int i = b.size() - 1; i >= 0; i--, j--)
//(b[i] - '0') is nothing but numerical value of string b char
a[j] += (b[i] - '0'); // if(b[i] ascii value is 49 then we subtract ascii value of '0'== 48 from it.
  
// be store all the values to a string now check ascii value of a[i]
for(int i = a.size()-1; i > 0; i--) {
if (a[i] > '9') { // if a[i] is greater than ascii value of '9' means value must be greater than 9
int d = a[i] - '0'; // calculate value of perticular digit by subtracting ascii value of a[i]-'9'
a[i-1] = ((a[i-1] - '0') + d/10) + '0'; // carry will be added to previous char values
a[i] = (d%10) + '0'; //current a[i] chang to (d%10) + '0'. if d is 11 than a[i] is 1 and carray be 1 added tp previous char.
}
}
  
if(a[0] > '9') // if first char ascii value is grater than ascii value of '9' then ther is a another char has to be added
{
string c; // carry of the first digits
c = a[0];// first store c as a[0]
a[0] = ((a[0]-'0')%10)+'0'; // now change a[0] as modulo 10 of the value
c[0] = ((c[0]-'0')/10)+'0'; // and chnge c to carry of a[0]
a = c + a; // now concatinate string c with string a
}
return a;
  
}

int main(){
   ifstream in ("input.in");
if (in.is_open())
{
string a,b;
int count=0;
while(in>>a>>b)   
cout <<"sample "<<++count<<" output: "<< addThese(a,b) <<"\n";
in.close();
}
else cout << "Unable to open file";

  
return 0;
}

Solutions

Expert Solution

Solution:

C code:

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

char* swap(char *a, char *b)
{
   char temp[100];
    strcpy(temp, a);
   strcpy(a, b);
    strcpy(b, temp);

}
// Function for finding sum of two large numbers
char* addThese( char a[], char b[]){

   // Before proceeding further, make sure length
   // of a is larger.
   if(strlen(a) < strlen(b))
       swap(a,b);

   int j = strlen(a) - 1;
   // Traverse from end of both , char *s and add (b[i] - '0') to a[j].
   for(int i = strlen(b) - 1; i >= 0; i--, j--)
       //(b[i] - '0') is nothing but numerical value of , char * b char
       a[j] += (b[i] - '0'); // if(b[i] ascii value is 49 then we subtract ascii value of '0'== 48 from it.

   // be store all the values to a , char * now check ascii value of a[i]
   for(int i = strlen(a)-1; i > 0; i--) {
       if (a[i] > '9') { // if a[i] is greater than ascii value of '9' means value must be greater than 9
           int d = a[i] - '0'; // calculate value of perticular digit by subtracting ascii value of a[i]-'9'
           a[i-1] = ((a[i-1] - '0') + d/10) + '0'; // carry will be added to previous char values
           a[i] = (d%10) + '0'; //current a[i] chang to (d%10) + '0'. if d is 11 than a[i] is 1 and carray be 1 added tp previous char.
       }
   }

   if(a[0] > '9') // if first char ascii value is grater than ascii value of '9' then ther is a another char has to be added
   {
       char c[100]; // carry of the first digits
       strcpy(c, &a[0]);// first store c as a[0]
       a[0] = ((a[0]-'0')%10)+'0'; // now change a[0] as modulo 10 of the value
       c[0] = ((c[0]-'0')/10)+'0'; // and chnge c to carry of a[0]
       strcat(a, c); // now concatinate , char * c with string a
   }
   return a;

}

int main(){
  
   FILE *fd = fopen("input.in", "rw");
   if (fd != NULL)
   {
       char a[100],b[100];
       int count=0;

       //while(fd>>a>>b)
       while(EOF)
           printf("sample %d, output: %s\n",++count, addThese(a,b));
       fclose(fd);
   }
   else {
       printf("Unable to open file");
       return 1;
   }


   return 0;
}

----------------------------------------------------------------------------------

Code screenshots:

-------------------------------------------------------------------------

Output:

------------------------------------------------------------------------


Related Solutions

I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() {...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() { char repeat = 'Y'; for (;repeat == 'Y';){ char empname[222]; float basicSalary, h_r_a, DearnessAllow, tax, netSalary; int e_id; cout<<"\nEmployee Name :"; cin>>empname; cout<<"\nEmployee Id :"; cin>>e_id; cout << "Enter Basic Salary : "; cin >> basicSalary; DearnessAllow = 0.30 * basicSalary; h_r_a= 800; switch (1) { case 1: if (basicSalary <= 2,50,000) tax = 0; case 2: if (basicSalary > 250000 && basicSalary <=...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /*     This code closely mimics the Python hash table we created in class this     week. Each "person" is defined by their name, zipcode, and their pet's name.     Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int); //---------------------------------------------------------------------------- /* define a "person" --------------------*/ class person{     public:     // variables that define a person     string name;     int zipcode;...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to...
(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to create a Priority Queue Follow these steps: Declare an integer array to use for the heap (size of 20 to 30 elements) Create an insertion function (and any needed helper functions) that creates a min-heap (smallest priority is most important) Create a remove function (and any needed helper functions) that removes the min value from the heap and returns it In main, call the...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std;...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return 0; }
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT