Assignment 1: 1. Convert String To Interger Without Using Atoi Function

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

JANANI.

R
CB.EN.U4EEE17122

ASSIGNMENT 1

1. CONVERT STRING TO INTERGER WITHOUT USING ATOI


FUNCTION:
#include<stdio.h>

int convert(char[] );
int main(){

char str[10];
int output;

printf("Enter any integer as a string: ");


scanf("%s",str);

output = convert(str);

printf("Equivalent integer value: %d",output);

return 0;
}

int convert(char str[]){


int i=0,integer=0;

while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
integer = integer*10 + (str[i] - 48);
i++;
}

return integer;

}
 This problem is solved using the ASCII value of the numbers.
 We convert the value of the string into integer and repeat the process until Last
value of string is reached.
Time Complexity: O(n)
JANANI.R
CB.EN.U4EEE17122

2. VOLUME OF CYLINDER USING COMMAND LINE


ARGUMENTS:

#include <stdio.h>
#include<string.h>
int main(int argc,char* argv[]) {
int arr[argc],i,volume=1;
for(i=1;i<argc;i++)
{
arr[i]=atoi(argv[i]);
volume=volume*arr[i];
}
volume=volume*arr[2];
printf(" Volume of Cylinder is %d",&volume);
return 0;
}
 The input is provided in the order pi (3.14), radius and height as strings
in the command line.
 Using atoi , string is converted into integer and multiplied.
 Time complexity: O(n).

3. SEARCH INSERT POSITION:


int searchInsert(int* nums, int numsSize, int target){

int i,count=0;
for(i=0;i<numsSize;i++)
{
if(nums[i]==target)
{
count=i;

}
}
if(count==0)
{
for(i=0;i<numsSize;i++)
{
if(nums[i]<target)
{
count=i+1;
}
}
}
return count;
}
JANANI.R
CB.EN.U4EEE17122

 Search for the element in the array


 If found, return the index.
 If the count is zero search for the element less than the target number
then return the next number with this index.
 Time Complexity : O(n).

4. MAX CONSECUTIVE ONES:

int findMaxConsecutiveOnes(int* nums, int numsSize){

int k = 0;
int sum = 0;
for (int i=0; i<numsSize; i++)
{
sum = (sum+nums[i])*nums[i];
if(k<sum)
{
k=sum;
}
}
return k;

}
 By incrementing count, we find consecutive ones.
 If zero is encountered count is made.
 The count value is saved in a temporary value and returned if count is
zero.
 Time Complexity: O(n).

5. CREATE TARGET ARRAY IN GIVEN ORDER:

int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int*
returnSize){

int i,j;
int *target = (int *)malloc(sizeof(returnSize)*indexSize);
for(i=0;i<numsSize;i++)
{
for(j=numsSize-1;j>index[i];j--)
{
target[j] = target[j-1];
}
target[index[i]] = nums[i];
}
*returnSize = numsSize;
return (target);
}
JANANI.R
CB.EN.U4EEE17122

 Here we check the repetition of indexes if repeated the maximum number is placed first.
 We use malloc function to create the array.

6. FIND NUMBERS WITH EVEN NUMBER OF DIGITS:

int findNumbers(int* nums, int numsSize){

int result = 0;

for(int i = 0;i < numsSize; i++){


int j=0;
int temp = nums[i];
while(temp!=0){
temp/=10;
j++;
}
if(j%2==0){
result++;
}
}
return result;
}

 We find the number of digits by dividing that number by 10.


 If the no of digits is divisible by 2 then increment the count by 1.
 Return count
 Time Complexity: O(n).

7. FIND LUCKY NUMBER IN AN ARRAY

int findLucky(int* arr, int arrSize){


    int i,j,count=1,temp[arrSize],max=0;
    for(i=0;i<arrSize;i++)
    {
        for(j=i+1;j<arrSize;j++)
        {
            if(arr[i]==arr[j])
            {count++;}
        }
JANANI.R
CB.EN.U4EEE17122

        if(arr[i]==count)
        {temp[i]=arr[i];}
        count=1;
    }
    for(i=0;i<arrSize;i++)
    {
        if(temp[i]>max)
        {max=temp[i];}
    }
    if(max<1)
    {return -1;}
    else
    {return max;}
}

 Create a array for frequency and save the number of the occurrences in the
index.
 If index and number in the index is equal then it is a lucky number.
 Time Complexity: O(n2).

You might also like