Snippets Collections
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#define STR_LEN 129
#define MAX_HEX_COUNT 30
#define _CRT_SECURE_NO_WARNINGS

//1.
int main()
{
	char znak = '\0';

	printf("Unesi znak:\t\n ESC za prekid.\n");
		do {
			znak = _getche();
		} while (znak != 27);
}
//2.
int main()
{
	char original[STR_LEN] = "\0", copy[STR_LEN] = "\0";
	int i = 0, j=0;

	printf("Unesi tekst:\n");
	scanf(" %s", original);

	for (i = 0; i < strlen(original); i++) {
		if (!isdigit(original[i])) {
			copy[j] = original[i];
			j++;
		}
	}
	copy[j] = '\0';
	printf("%s", copy);
}
//3.
int main()
{
	char txt[STR_LEN] = "\0";
	int i = 0, j = 0;
	char upper = '\0';

	printf("Unesi tekst:\t");
	scanf(" %s", txt);
	//a)
	for (i = 0; i < strlen(txt); i++) {
		if (isalpha(txt[i])) {
			txt[j++] = txt[i];
		}
	}
	txt[j] = '\0';
	printf("Nakon prve operacije: %s\n", txt);
	//b
	j = 0;
	for (i = 0; i < strlen(txt); i++) {
		switch (toupper(txt[i])) {
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			break;
		default:
			txt[j++] = txt[i];
		}
	}
	txt[j] = '\0';
	printf("Nakon druge operacije: %s\n", txt);
//c
	for (i = 0; i < strlen(txt); i++) {
		if (isupper(txt[i])) {
			txt[i] = tolower(txt[i]);
		}
		else {
			txt[i] = toupper(txt[i]);
		}
	}
	printf("Nakon trece operacije: %s\n", txt);
}

//4.
int main() {
	int decVal = 0, i=0, temp=0;
	char revhexVal[MAX_HEX_COUNT] = "\0", hexMap[]="0123456789ABCDEF";

	printf("Unesi dekadski broj:\t");
	scanf(" %d", &decVal);

	while (decVal != 0) {
		revhexVal[i++] = hexMap[decVal % 16];
		decVal /= 16;
	}
	i--;
	printf("Hex broj:\n");
	while (i >= 0) {
		printf("%c\n", revhexVal[i--]);
	}
}
//1.
int main()
{
	int value[2] = { 0 };
	char op = '\0';
	double result = 0.0;
	
	printf("Unesi prvi broj:\n");
	scanf(" %d", &value[0]);
	printf("Unesi drugi broj:\n");
	scanf(" %d", &value[1]);
	printf("Odaberi operaciju:\n + -> zbrajanje\n - -> oduzimanje\n * -> mnozenje\n / -> dijeljenje\n");
	scanf(" %c", &op);
 
	switch (op) {
	case '+':
		result = value[0] + value[1];
		break;
	case '-':
		result = value[0] + value[1];
		break;
	case '*':
		result = value[0] * value[1];
		break;
	case '/':
		if (value[1] == 0) {
			printf("Nije moguce dijelit s nulom!\n");
			result = INT_MIN;
		}
		else {
			result = (double)value[0] / value[1];
		}
		break;
	default:
		printf("Pogresan unos!\n");
		result = INT_MIN;
	}
	printf("------------\n");
	printf("Izvrsena operacija: %c\n", op);
	printf("Konacan rezultat: %lf\n", result);
}
//2.
int main()
{
	int ocjene[20] = { 0 }, i = 0;
	int histogram[5] = { 0 };
 
	printf("Potrebno je unijeti 20 ocjena.\n");
	for (i = 0; i < 20; i++) {
		printf("Ocjena broj %d.\t", i + 1);
		scanf(" %d", &ocjene[i]);
		if (ocjene[i] < 1 || ocjene[i] > 5) {
			printf("Neispravan unos!\n");
			i--;
		}
	}
	for (i = 0; i < 20; i++) {
		histogram[ocjene[i] - 1]++;
	}
	printf("------------\n");
	for (i = 0; i < 5; i++) {
		printf("Broj ocjena %d: %d\n", i + 1, histogram[i]);
	}
}
//3.
int main()
{
	int niz[10] = { 0 };
	int i=0, j = 0;
	int min = 0, max = 0;
	int temp = 0;
 
	printf("Unesi 10 brojeva:\n");
	for (i = 0; i < 10; i++) {
		printf("Broj %d:\t", i + 1);
		scanf(" %d", &niz[i]);
	}
	min = niz[0];
	max = niz[0];
	for (i = 1; i < 10; i++) {
		if (niz[i] < min) {
			min = niz[i];
		}
		if (niz[i] > min) {
			max = niz[i];
		}
	}
	for (i = 0; i < 9; i++) {
		for (j = i + 1; j < 10; j++) {
			if (niz[i] > niz[j]) {
				temp = niz[i];
				niz[i] = niz[j];
				niz[j] = temp;
			}
		}
	}
	printf("--------------\n");
	printf("Minimalni broj: %d\n Maksimalni broj: %d\n", min, max);
	for (i = 1; i < 10; i++) {
		printf("%d\n", niz[i]);
	}
}
int main()
{
	int arr[5] = { 0 };
	int i = 0, j = 0, temp = 0;

	printf("Unesi 5 brojeva:\n");
	for (i = 0; i < 5; i++) {
		scanf(" %d", &arr[i]);
	}
	for (i = 0; i < 4; i++) {
		for (j = i + 1; j < 5; j++) {
			if (arr[i] > arr[j]) {
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
	printf("------------\n");
	for (i = 0; i < 5; i++) {
		printf("%d\n", arr[i]);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS

//1.
int main()
{
	float num = 0.0;
	int wholenum = 0;

	printf("Unesi realni broj:\n");
	scanf_s("%f", &num);
	wholenum = num;
	if (num >= 0) {
		if (num - wholenum >= 0.5)
		{
			wholenum++;
		}
	}
	else if (num - wholenum <=-0.5) {
		wholenum--;
	}
	printf("Cjelobrojna vrijednost je %d.\n", wholenum);
	return 0;
}
//3.
int main()
{
	int broj, temp = 0;
	for (int i = 0; i < 10; i++)
	{
		printf("Unesi %d. broj.\n", i + 1);
		scanf("%d", &broj);
		printf("\n");
		if (broj % 3 == 0)
			temp++;
	}

	printf("%d unesena/ih brojeva djeljivo je s 3.\n", temp);
}
//4.
int main()
{
	char slovo; int br_H = 0, br_ms = 0;
	for (int i = 0; i < 10; i++)
	{
		printf("Unesi slovo:\t");
		scanf(" %c", &slovo);
		printf("\n");
		if (slovo == 'H')
			br_H++;
		if (slovo >= 'a' && slovo <= 'z')
			br_ms++;
	}

	printf("Slovo H uneseno je %d puta.\n", br_H);
	printf("Malo slovo uneseno je %d puta.\n", br_ms);
}
//5.
int main()
{
	int pass = 0, score = 0,cnt=0, i = 0; float avg = 0.0f;
	for (i = 0; i < 5; i++) {
		printf("Broj bodova:\t");
		scanf(" %d", &score);
		if (score < 0 || score > 100) {
			i--;
			continue;
		}
		if (score >= 50) {
			cnt++;
		}
		pass += score;
	}
		printf("Broj prolaznika: %d\n", cnt);
		avg = pass / 5.0f;
		printf("Prosjecan broj bodova: %f", avg);
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
//1.
int main() {
	int cnt = 0, sum = 0, num = 0; float aritm = 0.0;
 
	do {
		printf("Unesi broj\t");
		scanf("%d", &num);
		if (num == 0) {
			break;
		}
		cnt++;
		sum += num;
	} while (1);
	aritm = (float)sum / cnt;
	printf("Aritmeticka sredina je %f.\n", aritm);
}
//2.
int main()
{
	int br = 0, rem = 0;
	printf("Unesi broj:\t");
	scanf(" %d", &br);
	if (br < 0) {
		printf("- ");
		br *= -1;
	}
	do {
		rem = br % 10;
		printf("%d ", rem);
		br /= 10;
	} while (br != 0);
}
//3.
int main()
{
	int rnd = 0, guess = 0, cnt = 0;
 
	srand((unsigned)time(NULL));
	rnd = rand();
	do {
		printf("Unesi broj:\t");
		scanf(" %d", &guess);
		cnt++;
		if (guess < rnd) {
			printf("Random broj je veci od unesenog!\n");
		}
		else if (guess > rnd) {
			printf("Random broj je manji od unesenog!\n");
		}
 
	} while (rnd != guess);
	printf("BINGO!\n");
	printf("Potrebno je bilo %d pokusaja.\n", cnt);
}
#include <stdio.h>

int main(){
  char str[20];
  
  fgets(str, 20, stdin); // read from stdin
  puts(str); // print read content out to stdout
  
  // open the file
  FILE *f = fopen("file.txt" , "r"); 
  
  // if there was an error
  if(f == NULL){
    perror("Error opening file"); // print error
    return(-1); 
  }
  // if there was no error
  else{ 
    fgets(str, 20, f); // read from file
    puts(str); // print read content out to stdout
  }
  
  fclose(f); // close file
  
  return(0);
}
#include "SevSeg.h"
SevSeg sevseg;

int LEDRa = 33;
int LEDGa = 32;

int LEDRb = 23;
int LEDGb = 22;

int LEDRc = 50;
int LEDGc = 51;

int LEDRd = 42;
int LEDGd = 43;

int IRa1 = 44;
int IRa2 = 52;

int IRb1 = 49;
int IRb2 = 48;

int IRc1 = 35;
int IRc2 = 37;

int IRd1 = 47;
int IRd2 = 46;

int RoadATraffic[] = {0, 0};
int RoadBTraffic[] = {0, 0};
int RoadCTraffic[] = {0, 0};
int RoadDTraffic[] = {0, 0};

char jam[] = {'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};

void setup() {
  pinMode (LEDRa, OUTPUT);
  pinMode (LEDGa, OUTPUT);

  pinMode (LEDRb, OUTPUT);
  pinMode (LEDGb, OUTPUT);

  pinMode (LEDRc, OUTPUT);
  pinMode (LEDGc, OUTPUT);

  pinMode (LEDRd, OUTPUT);
  pinMode (LEDGd, OUTPUT);

  pinMode (IRa1, INPUT);
  pinMode (IRa2, INPUT);

  pinMode (IRb1, INPUT);
  pinMode (IRb2, INPUT);

  pinMode (IRc1, INPUT);
  pinMode (IRc2, INPUT);

  pinMode (IRd1, INPUT);
  pinMode (IRd2, INPUT);

  byte numDigits = 2;
  byte digitPins[] = {11, 10};
  byte segmentPins[] = {9, 8, 6, 3, 4, 7, 2, 5};
  bool resistorsOnSegments = true;

  byte hardwareConfig = COMMON_CATHODE;
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);

  Serial.begin(9600); // open the serial port at 9600 bps:
}

static int counter = 5;
int jam_ind = 0;
int current_route = 0;

void loop() {
    //display();
    RoadATraffic[0] = (digitalRead (IRa1) != 1) ? ( 1 ) : ( 0 );
    RoadATraffic[1] = (digitalRead (IRa2) != 1) ? ( 1 ) : ( 0 );

    RoadBTraffic[0] = (digitalRead (IRb1) != 1) ? ( 1 ) : ( 0 );
    RoadBTraffic[1] = (digitalRead (IRb2) != 1) ? ( 1 ) : ( 0 );

    RoadCTraffic[0] = (digitalRead (IRc1) != 1) ? ( 1 ) : ( 0 );
    RoadCTraffic[1] = (digitalRead (IRc2) != 1) ? ( 1 ) : ( 0 );

    RoadDTraffic[0] = (digitalRead (IRd1) != 1) ? ( 1 ) : ( 0 );
    RoadDTraffic[1] = (digitalRead (IRd2) != 1) ? ( 1 ) : ( 0 );

    int sum1 = RoadATraffic[0] + RoadATraffic[1];
    int sum2 = RoadBTraffic[0] + RoadBTraffic[1];
    int sum3 = RoadCTraffic[0] + RoadCTraffic[1];
    int sum4 = RoadDTraffic[0] + RoadDTraffic[1];

    Serial.print(current_route);
    Serial.print('\n');
    Serial.print(jam_ind);
    Serial.print('\n');
    Serial.print('\n');

    change_route(jam[current_route]);
    display();
      
    if (sum1 >= sum2 && sum1 >= sum3 && sum1 >= sum4) {     // Road A Jam
      if (jam[current_route] == 'a' && counter == 1) {
        increase_jam_ind();
        jam[jam_ind] = 'a';
      } else if (jam[current_route] != 'a' && jam[jam_ind] != 'a') {
        increase_jam_ind();
        jam[jam_ind] = 'a';
      }
    }

    if (sum2 >= sum1 && sum2 >= sum3 && sum2 >= sum4) { // Road B Jam
      if (jam[current_route] == 'b' && counter == 1) {
        increase_jam_ind();
        jam[jam_ind] = 'b';
      } else if (jam[current_route] != 'b' && jam[jam_ind] != 'b') {
        increase_jam_ind();
        jam[jam_ind] = 'b';
      }
    }
    
    if (sum3 >= sum1 && sum3 >= sum2 && sum3 >= sum4) { // Road C Jam
      if (jam[current_route] == 'c' && counter == 1) {
        increase_jam_ind();
        jam[jam_ind] = 'c';
      } else if (jam[current_route] != 'c' && jam[jam_ind] != 'c') {
        increase_jam_ind();
        jam[jam_ind] = 'c';
      }
    }
      
    if (sum4 >= sum1 && sum4 >= sum2 && sum4 >= sum3) { // Road D Jam
      if (jam[current_route] == 'd' && counter == 1) {
        increase_jam_ind();
        jam[jam_ind] = 'd';
      } else if (jam[current_route] != 'd' && jam[jam_ind] != 'd') {
        increase_jam_ind();
        jam[jam_ind] = 'd';
      }
    }  
}

void display() {
  for (int h = 1; h<10000; h++){
    sevseg.setNumber(counter);
    sevseg.refreshDisplay();  
  }
  if (counter <= 0) {
    counter = 5;
    current_route++;
  }

  if (current_route > 15) { 
    current_route = 0;
  }
  
  counter--;
}

void change_route(char route) {
  switch (route) {
    case 'a':
      digitalWrite(LEDGa, HIGH);
      digitalWrite(LEDRa, LOW);
    
      digitalWrite(LEDGb, LOW);
      digitalWrite(LEDRb, HIGH);
    
      digitalWrite(LEDGc, LOW);
      digitalWrite(LEDRc, HIGH);
    
      digitalWrite(LEDGd, LOW);
      digitalWrite(LEDRd, HIGH);
      break;

    case 'b':
      digitalWrite(LEDGa, LOW);
      digitalWrite(LEDRa, HIGH);
    
      digitalWrite(LEDGb, HIGH);
      digitalWrite(LEDRb, LOW);
    
      digitalWrite(LEDGc, LOW);
      digitalWrite(LEDRc, HIGH);
    
      digitalWrite(LEDGd, LOW);
      digitalWrite(LEDRd, HIGH);
      break;

    case 'c':
      digitalWrite(LEDGa, LOW);
      digitalWrite(LEDRa, HIGH);
    
      digitalWrite(LEDGb, LOW);
      digitalWrite(LEDRb, HIGH);
    
      digitalWrite(LEDGc, HIGH);
      digitalWrite(LEDRc, LOW);
    
      digitalWrite(LEDGd, LOW);
      digitalWrite(LEDRd, HIGH);
      break;

    case 'd':
      digitalWrite(LEDGa, LOW);
      digitalWrite(LEDRa, HIGH);
    
      digitalWrite(LEDGb, LOW);
      digitalWrite(LEDRb, HIGH);
    
      digitalWrite(LEDGc, LOW);
      digitalWrite(LEDRc, HIGH);
    
      digitalWrite(LEDGd, HIGH);
      digitalWrite(LEDRd, LOW);
      break;
  }
}

void increase_jam_ind() {
  jam_ind++;
  if (jam_ind > 15) {
    jam_ind = 0;
  }
}
#include <linux/limits.h>

char current_path[PATH_MAX];
star

Thu Apr 15 2021 18:12:30 GMT+0000 (UTC)

#c
star

Sun Apr 11 2021 18:16:25 GMT+0000 (UTC)

#c
star

Sun Apr 11 2021 15:56:01 GMT+0000 (UTC) https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm

#c
star

Sun Apr 11 2021 15:52:44 GMT+0000 (UTC)

#c
star

Sun Apr 11 2021 15:46:46 GMT+0000 (UTC)

#c
star

Wed Dec 09 2020 05:39:08 GMT+0000 (UTC) https://rud.is/b/2020/02/06/prying-r-script-files-away-from-xcode-et-al-on-macos/

#c
star

Sun Oct 04 2020 21:07:26 GMT+0000 (UTC) https://www.educative.io/edpresso/how-to-use-the-fgets-function-in-c

#c
star

Sun Sep 27 2020 09:35:30 GMT+0000 (UTC)

#c
star

Wed Jun 24 2020 08:57:25 GMT+0000 (UTC) https://stackoverflow.com/questions/9449241/where-is-path-max-defined-in-linux

#c

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension