Question
c class
ex in the 2pic
Question 1: A. Write a recursive function Ecursive function that converts a string of digits into an integer. The function re
charx = 1234: Sở 3 : void printlnt (charts) if( *s == () return; printf(%. Ch, ts): 7 printlnt (5+1): Ob..
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A)

CODE

#include<stdio.h>

#include <math.h>

int arrLength(char input[]){

  int len = 0;

  for(int i =0;input[i] != '\0';i++){

    len++;

  }

  return len;

}

int stringToInteger(char arr[], int lastIndex){

  //Base Case

  if(lastIndex == 0){

    return arr[lastIndex] - '0';

  }

  //Recursive Call

  int res = stringToInteger(arr, lastIndex-1);

  int a = arr[lastIndex] - '0';

  return res * 10 + a;

}

int main() {

char arr[] = {'1', '2', '3', '4'};

int length = arrLength(arr);

printf("%d", stringToInteger(arr, length-1));

}

B)

The time complexity of this method is O(N) where N is the number of digits in the number.

Add a comment
Know the answer?
Add Answer to:
c class ex in the 2pic Question 1: A. Write a recursive function Ecursive function that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **C++ only and no vectors or global variables. Thank you, Write a recursive function to convert...

    **C++ only and no vectors or global variables. Thank you, Write a recursive function to convert a character string of digits to an integer. Example: convert("1234) returns 1234. Hint: To convert a character to a number, subtract the ASCII value '0' from the character, then the function can return the value s[0] - '0'.

  • Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”)...

    Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’.

  • For each of the following recursive methods available on the class handout, derive a worst-case recurrence...

    For each of the following recursive methods available on the class handout, derive a worst-case recurrence relation along with initial condition(s) and solve the relation to analyze the time complexity of the method. The time complexity must be given in a big-O notation. 1. digitSum(int n) - summing the digits of integer: int digitSum(int n) {                 if (n < 10)                                 return n;                 return (digitSum(n/10) + n%10); } 2. void reverseA(int l, int r) - reversing array: void...

  • Do it in C please, without using parseint, atoi. * The isinteger ) function examines the...

    Do it in C please, without using parseint, atoi. * The isinteger ) function examines the string given as its first argument, and returns true if and only if the string represents a well-formed integer. A well-formed integer consists only of an optional leading - followed by one or more decimal digits. Returns true if the given string represents an integer, false otherwise. bool isinteger (char +str) *The parseint ) function parses a well-formed string representation of * an integer...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....

    Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C...

    C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C Strings: char *strStr(const char *s1, const char *s2) - Locates the string s2 as a substring of s1 and returns a pointer to its first occurrence in s1 or nullptr if s2 is not a substring of s1 char *strrChr(constant char *s, int ch) - Returns a pointer to the last occurrence of the character ch in string s or nullptr if ch is...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT