Wednesday, 16 September 2015

Factset Question: Subarray of k Size from a Array that has given Mean

/*
C program: print a sub-array b of size k that have mean m from a 
given array a of size n.

Description: Question asked in written (codding) test of FactSet
recruitment drive at NIT Durgapur Campus on September 14, 2016.

Example 1: int a[] = {1,2,3,4,5,6,7,8,9,0}, k = 4, m = 6
then b will be b[] = {7,8,9,0} and should print 7 8 9 0

Compiler: GNU GCC v4.8.3

Author: Mangilal Sharma (with Ramesh Kumar Thakur)

Date: September 15, 2015

Program for: world-of-c-programming.blogpost.com

Program number: 103
*/

#include <stdio.h>
#define SIZE 20
void findMeanSubarray(int a[], int k, int m, int n);

int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9,0}, k = 4, m = 6;
    int n = sizeof(a)/sizeof(a[0]);
    findMeanSubarray(a,k,m,n);
    return 0;
}
//Only the following function code required to write in test.
void findMeanSubarray(int a[], int k, int m, int n)
{
 int b[SIZE], i, j, flag = 0, sum;
 for(i=0;i<=n-k+1;i++)
 {
  sum=0;
  for(j=0;j<k;j++) sum+= a[i+j];
  if(sum == m*k) break;
 }
 if(i<=n-k) for(j=i;j<i+k;j++) printf("%d\t",a[j]);
 else printf("Has not found any required subarray in given array.\n");
}
You can test program on this online c editor & compiler: http://www.tutorialspoint.com/compile_c_online.php

Saturday, 12 September 2015

C program for User defined Custom atoi Function Code

/*

C program: atoi function custom

Description: atoi function convert string to integer representation of integral numbers.



Example 1: string "02302"    converts to number 2302

Example 2: string "abcd"     returns 0

Example 3: string "-0234"    converts to number -234

Example 4: string "0023ABCD" converts to number 23

Example 5: string "12abc45g" converts to number 12

Example 6: string "--0123"   returns 0

Example 7: string "" (NULL)  returns 0

Example 8: string "   234a"  converts to number 234

Example 9: string "+  239b"  returns 0

Example10: string "+239"     converts to number 239

Example11: string "   +12"   converts to number 12

Example12: string "   -12"   converts to number -12



Compiler: Turbo C++ 3.0

Author: Mangilal Sharma

Date: September 12, 2015

Program for: world-of-c-programming.blogpost.com

Program number: 102

*/



#include<stdio.h> //for printf and gets

#include<conio.h> //for clrscr and getch

#include<string.h> //for strcpy

typedef enum {false, true} bool; //To use bool data type



bool isNotNumericChar(char x) //to check the character

{

 return(x >= '0' && x<= '9')? false:true;

}



int myAtoi(char *str) //Cutom atoi function code

{

 int result = 0; //Initialize result variable

 int sign = 1; //Initialize sign as positive

 int i = 0; //index of first digit in string



 if(str == NULL) return 0;



 //If number is negative, then update sign

 while(str[i] == ' ') i++;



 if(str[i] == '-')

 {

  sign = -1;

  i++;

 }

 else if(str[i] == '+')

 {

  i++;

 }



 //Iterate through all digits of input string and update result value

 for(;str[i]!= '\0';i++)

 {

  if(isNotNumericChar(str[i])) break; //Break when non numeric char occurs

  //We can also use isdigit function from ctype.h instead of our custom function

  result = result*10 + str[i] - '0';

  //subtracted ASCII value of zero from ASCII value of current digit character

 }



 return sign*result;

}



int main()

{

 char string[] = "02302";

 int val = myAtoi(string);

 clrscr();

 printf("String value = %s, Int value = %d\n",string,val);

 strcpy(string,"C Program");

 val = myAtoi(string);

 printf("String value = %s, Int value = %d\n",string,val);



 printf("Press y for try your strings to atoi function.\n");

 printf("Press any other key to exit.\n");

 while(getch() == 'y')

 {

  printf("Enter String and press enter:");

  gets(string);

  printf("String value = %s, Int value = %d\n",string,myAtoi(string));

 }

 return 1;

}

C Program to Demonstrate use of atoi Function

/*

C program: atoi function use

Description: atoi function convert string to integer representation of integral numbers.



Example 1: string "02302"    converts to number 2302

Example 2: string "abcd"     returns 0

Example 3: string "-0234"    converts to number -234

Example 4: string "0023ABCD" converts to number 23

Example 5: string "12abc45g" converts to number 12

Example 6: string "--0123"   returns 0

Example 7: string "" (NULL)  returns 0

Example 8: string "   234a"  converts to number 234

Example 9: string "+  239b"  returns 0

Example10: string "+239"     converts to number 239

Example11: string "   +12"   converts to number 12

Example12: string "   -12"   converts to number -12



Compiler: Turbo C++ 3.0

Author: Mangilal Sharma

Date: September 12, 2015

Program for: world-of-c-programming.blogpost.com

Program number: 101

*/



#include<stdio.h> //for printf and gets

#include<conio.h> //for clrscr and getch

#include<stdlib.h> //for atoi

#include<string.h> //for strcpy



void main()

{

 char string[] = "02302";

 clrscr();

 printf("String value = %s, Int value = %d\n",string,atoi(string));

 strcpy(string,"C Program");

 printf("String value = %s, Int value = %d\n",string,atoi(string));



 printf("Press y for try your strings to atoi function.\n");

 printf("Press any other key to exit.\n");

 while(getch() == 'y')

 {

  printf("Enter String and press enter:");

  gets(string);

  printf("String value = %s, Int value = %d\n",string,atoi(string));

 }

}