Posts

Showing posts with the label c

C/C++ Integration with (ChatGPT)GPT-4: A Practical Example

  C/C++ Integration with GPT-4: A Practical Example Integrating C/C++ with GPT-4 (or ChatGPT) can offer powerful capabilities for applications requiring advanced natural language processing. While GPT-4 is typically accessed through high-level languages like Python, it’s possible to interface with it from C/C++ by using HTTP requests. This article will provide a step-by-step guide on how to achieve this integration, including code examples. Prerequisites OpenAI API Key : Obtain an API key from OpenAI by signing up for their services. HTTP Library : Use an HTTP library to make requests. For C/C++, common choices are libcurl or Boost.Beast . JSON Library : For parsing JSON responses, consider using libraries such as nlohmann/json for C++. Steps to Integrate GPT-4 with C/C++ Setup Your Project Ensure your project includes the necessary libraries. For instance, if using libcurl and nlohmann/json in C++, you would typically include them in your project configuration. Install Depende...

Integrating C/C++ with Gemini: A Comprehensive Guide

  Integrating C/C++ with Gemini: A Comprehensive Guide Introduction Gemini, developed by Google, is a powerful language model designed to perform various AI-driven tasks such as natural language understanding and generation. Integrating C/C++ with Gemini can unlock advanced functionalities and improve the performance of applications by leveraging the strengths of both technologies. This article provides a step-by-step guide on how to integrate Gemini with a C/C++ application, including practical examples. Prerequisites Before diving into the integration process, ensure you have the following: C/C++ Development Environment : An IDE or compiler setup for C/C++ (e.g., GCC, Clang, or Visual Studio). Gemini API Access : You need access to the Gemini API. Sign up on the Gemini platform to obtain your API key. JSON Library : A JSON parsing library for C/C++ (e.g., nlohmann/json for C++ or json-c for C). Steps for Integration 1. Setting Up Your C/C++ Project Create a new C or C++ project...

Java - Linear Search Example

Linear search, also called as sequential search, is a very simple method used for searching an array for a particular value. It works by comparing the value to be searched with every element of the array one by one in a sequence until a match is found. Linear search is mostly used to search an unordered list of elements (array in which data elements are not sorted). For example, if an array A[] is declared and initialized as, int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5}; and the value to be searched is VAL = 7, then searching means to find whether the value ‘7’ is present in the array or not. If yes, then it returns the position of its occurrence. Here, POS = 3 (index starting from 0). Algorithm for linear search: LINEAR_SEARCH(A, N, VAL) Step 1: [INITIALIZE] SET POS=-1 Step 2: [INITIALIZE] SETI=1 Step 3: Repeat Step 4 while I<=N Step 4: IF A[I] = VAL         SET POS=I          PRINT POS         Go to St...

C, C++ (malloc, calloc, free and realloc) different type of Memory allocation/de-allocation functions

malloc()  Allocates memory and returns a pointer to the first byte of allocated space. The general syntax of malloc() is ptr =(cast-type*)malloc(byte-size); Example: arr=(int*)malloc(10*sizeof(int)); calloc()  Allocates space for an array of elements, initializes them to zero and returns a pointer to the memory. The syntax of calloc() can be given as: ptr=(cast-type*) calloc(n,elem-size); free() Frees previously allocated memory. The general syntax of the free()function is, free(ptr); realloc()  Alters the size of previously allocated memory. The general syntax for realloc() can be given as, ptr = realloc(ptr,newsize);

Inserting a Node at the End of a Linked List

Suppose we want to add a new node with data as the last node of the list. Algorithm to insert a new node at the end of a linked list Step 1: IF AVAIL = NULL           Write OVERFLOW           Go to Step 10         [END OF IF] Step 2: SET NEW_NODE = AVAIL Step 3: SET AVAIL = AVAIL.NEXT Step 4: SET NEW_NODE.DATA = VAL Step 5: SET NEW_NODE.NEXT = NULL Step 6: SET PTR = START Step 7: Repeat Step 8 while PTR NEXT != NULL Step 8: SET PTR = PTR.NEXT        [END OF LOOP] Step 9: SET PTR NEXT = NEW_NODE Step 10: EXIT we take a pointer variable PTR and initialize it with START. That is, PTR now points to the first node of the linked list. In the while loop, we traverse through the linked list to reach the last node. Once we reach the last node, in Step 9, we change the NEXT pointer of the last node to store the address of the new node. Remember that the NEXT field of the new node contain...

Linked List Cycle Problems - Given a linked list, determine whether there are rings/circle in the linked list.

Linked List Cycle Problems - Given a linked list, determine whether there are rings/circle in the linked list. Title description: Given a linked list, determine whether there are rings in the linked list. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the tail of the linked list is connected to the linked list (the index starts at 0). If pos is -1, there is no ring in the list. Map set solution Idea: Create a map collection, key is the node, and value is the address value. Because ListNode does not rewrite the toString() method, the content returned by the toString() method is used as the value. If there is content returned by the toString() method of the current node in the map, there is a ring. /**  * Definition for singly-linked list.  * class ListNode {  *     int val;  *     ListNode next;  *     ListNode(int x) {  *  ...

Linked List Cycle Problems - Given a linked list, return to the first node of the linked list that begins to enter the ring

Linked List Cycle Problems - Given a linked list, return to the first node of the linked list that begins to enter the ring Topic description: Given a linked list, return to the first node of the linked list that begins to enter the ring. If the linked list has no rings, it returns null. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the tail of the linked list is connected to the linked list (the index starts at 0). If pos is -1, there is no ring in the list. Note: It is not allowed to modify the given linked list. Formula calculation: It is easy to get: m=x+y (two representations of the length of the ring); the fast pointer takes two steps and the slow pointer takes one step. So the speed of the fast pointer is twice the speed of the slow pointer, so in the same time, the length of the fast pointer is twice the length of the slow pointer: yes: f=2s; after the fast pointer walks through the circle, the two po...

What is top down programing?

it is a software development technique that imposes a hierarchical structure on the design of the program. It starts out by defining the solution at the highest level of functionality and breaking it down further and further into small routines that can be easily documented and coded.  Also called " stepwise refinement". structured programming Techniques that impose a logical structure to the coding of a program in order to make it easy to follow. Large routines are broken down into small modules that have to be methodically and carefully nested within each other, because the use of the GOTO statement is either discouraged or not available in the language. Structured walkthroughs, which invite criticism from peer programmers, are also used. Structured languages, such as Pascal, Ada and dBASE, force the programmer to write a structured program; however, all programming languages can be written in a structured manner.

Convert String to an int

How to convert a String to an int ? String str = "1234"; Integer x = Integer.valueOf(str); // or int y = Integer.parseInt(str);

write a program to check if strings are rotations of each other or not

check number is power of 2 or not

given n=200 Check this number is power of two or not Solution: 1 |  01 2 |  10 3 |  011 4 |  100 5 |  101 6 |  110 7 |  0111 8 |  1000 Check the binary digit of the number with power  if we do n&n-1, Result will be 0 so, n&n-1 result is not 0, then that number not a power of two

Producer and Consumer Problem Solution Using Multi threading

How to solve Producer and Consumer problem in Java using thread wait and notify method Best example Producer Consumer problem can be easily solved by using Thread Concept where one thread is used to produce and another thread is used to consume. Here the wait and notify method will be used for inter-thread communication to notify other party (Producer by Consumer thread and vice-versa). To solve this problem, Using ArrayList as shared object, where producer thread puts the object (Just Integer in this case) and consumer thread consumes the object (integer). Interestingly, Here using same list object as monitor lock, thus removing the additional overhead to create shared monitor lock. Example: import java.util.ArrayList; import java.util.*;   public class ProducerConsumer {  public static void main (String[] args) {   ArrayList<Integer> list = new ArrayList<Integer>();   Thread t1 = new Thread(new Producer(list));   Thread t2 = new Thread(new Consum...

String get all content between two characters

          You can get content between symbols even there are other same characters. You can extract a string between two delimiters or two string or symbol. Example : String-> Some Text [Id-123] Output-> Id-123 You can achieve same functionality with out using Regular expression. You can use String operaton. For any programming like Java, JavaScript, C, C++, .Net, AngularJs, You can Use String operation. String str = "Some Text [Id-123]"; String str2 =  str.substring(str.lastIndexOf("[") + 1, str.lastIndexOf("]"));  Output-> Id-123 It will work for any symbol or characters.

java number to word converter

        You can Convert Math Number to Word, It is helpful when developing a payment system if they want invoice in readable English word.  Java Program:

convert string to int without parseint

Convert string to integer without using build in api Integer.parseInt()    Using this concept we can convert String to Number, String to Float, String to Double, String to short and long                                                         ASCII code of decimal number start form  48 public class ConvertStringToInt { public static void main(String[] args) { String str="-14545"; int total=0; boolean m=false; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='-'){ m=true; continue; } if(str.charAt(i)=='+'){ continue; } total=(total*10)+(str.charAt(i)-48); } if(m){ total=0-total; } System.out.println(total); } }

Chance problems program

Generating Chance of getting 0 is 75% percent and 1 is 25% percent probability import java.util.Random; public class Chance { public static int getChance(){ Random randomNumber = new Random(); int randomGenerator = randomNumber.nextInt(100); System.out.println(randomGenerator); if(randomGenerator < 75) return 0; else  return 1; } public static void main(String[] args) { for(int i=0;i<10;i++){ System.out.println(Chance.getChance()); } } }

java : operator example

java : operator example public class Array2 { public static void main(String[] args) { int[] arr={1,2,3,4,5,0,0,1,3}; Outer:for(int i=0;i<arr.length;i++){ Innter: for(int j=i+1;j<arr.length;j++){ if(arr[i]==arr[j]){ System.out.println(arr[i]); break Outer; } } } /*String s="ffaghglklklh"; Outer:for(int i=0;i<s.length();i++){ Innter: for(int j=i+1;j<s.length();j++){ if(s.charAt(i)==s.charAt(j)){ System.out.println(s.charAt(i)); break Outer; } } }*/ } }

Java Outer and Inner loop example

Program to find or print first repeated/duplicate element public class Array { public static void main(String[] args) { int[] arr={1,2,3,4,5,0,0,1,3}; Outer:for(int i=0;i<arr.length;i++){ Innter: for(int j=i+1;j<arr.length;j++){ if(arr[i]==arr[j]){ System.out.println(arr[i]); break Outer; } } } /*String s="ffaghglklklh"; Outer:for(int i=0;i<s.length();i++){ Inner: for(int j=i+1;j<s.length();j++){ if(s.charAt(i)==s.charAt(j)){ System.out.println(s.charAt(i)); break Outer; } } }*/ } }

Web crawlers

Web crawlers (web spider)  is a search engines software use to traverse the Internet and to save each of the individual pages passed by along the way. Search engines then use additional software to index each of the saved pages, creating a database containing all the words in the pages. Internet search engines like Google, Yahoo and Bing can search the Internet on topic and return a list of results so quickly. Obviously it would be impossible to search all the Web pages for each request in real time. Search engine indexed Web pages into highly optimized databases and search matches the keyword each search.

program to remove multiple white spaces from a string

Remove multiple extra white spaces from a string if in present in between a string public class WhiteSpace { public static String removeSpace(String string) { String str = ""; int length = string.length(); if (length <= 0) { return str; } boolean start = true; boolean first = false; for (int i = 0; i < length; i++) { char c = string.charAt(i); if (c == ' ') { if (start) { if(first) str += c; start = false; } }else { first=true; str += c; start = true; } } return str.trim(); } public static void main(String[] args) { String str="  g  fdf  dddd      dfd  g   sdfasdf      ff"; System.out.println(removeSpace(str)); } }