Assignment 1: 1. Convert String To Interger Without Using Atoi Function
Assignment 1: 1. Convert String To Interger Without Using Atoi Function
Assignment 1: 1. Convert String To Interger Without Using Atoi Function
R
CB.EN.U4EEE17122
ASSIGNMENT 1
int convert(char[] );
int main(){
char str[10];
int output;
output = convert(str);
return 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
#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).
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
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).
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.
int result = 0;
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).