0% found this document useful (0 votes)
44 views6 pages

Probleme Examen

The document contains code snippets in C programming language for various tasks like creating and traversing linked lists, matrix input/output, sorting numbers, finding perfect numbers, dynamic memory allocation using malloc and realloc, and string manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

Probleme Examen

The document contains code snippets in C programming language for various tasks like creating and traversing linked lists, matrix input/output, sorting numbers, finding perfect numbers, dynamic memory allocation using malloc and realloc, and string manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

int main() {
int n,i,j;
printf("Introduisez la dimension de la matrice:");
scanf("%d", &n);
int *array[n];
for(i = 0; i < n; i++)
array[i] = (int*)malloc(n*sizeof(int));
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("array[%d][%d]=", i,j);
scanf("%d", &array[i][j]);
}
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("array[%d][%d]=", i,j);
printf("%d\n", array[i][j]);
}
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* fun(char **p){


char *t;
t = (p+=2)[-1];
return t;
}
int main() {
char *arv[] = {"ab","cd","ef","gh"};
printf("%s", fun(arv));
return 0;
}
 Afiseaza cd

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char s[7] = "IndiaBIX";
printf("%s", s);
return 0;
}
Afiseza IndiaBI����

Bonus
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int a,b;
printf("Introduisez a:");
scanf("%d",&a);
printf("Introduisez b:");
scanf("%d",&b);
if(a > b){
const int n = a-b;
a = a-n;
b = b+n;
}
else{
const int m = b-a;
a = a+m;
b = b-m;
}
printf("a=%d\nb=%d",a,b);
return 0;
}

Varianta 2

Creare, adaugare si afisare lista


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct lista{
char elem;
struct lista *next;
};

void adaugare(struct lista *l1, char elem){


while(l1->next != NULL)
l1 = l1->next;
l1->next = malloc(sizeof(struct lista));
l1->next->elem = elem;
l1->next->next = NULL;

void afisare(struct lista *l1){


if(l1 != NULL){
printf("%c", l1->elem);
afisare(l1->next);
}
}

int main() {
struct lista *list;
list = malloc(sizeof(struct lista));
printf("Introduisez le premier element de la liste:");
scanf("%c", &list->elem);
list->next = NULL;
adaugare(list, 'c');
afisare(list);
return 0;
}

Nr. Perfecte pana la un numar dat de utilizator


#include <stdio.h>

int main() {
int n,i,j;
printf("Introduceti numarul pana la care doriti sa se afiseze numerele
perfecte:");
scanf("%d", &n);
for(i = 1; i <= n; i++){
int sum = 0;
for(j = i-1; j > 0; j--)
if(i % j == 0)
sum = sum + j;
if(sum == i)
printf("%d ", i);
}
return 0;
}

Varianta 3

Se creeaza o lista in care se va afisa numarul introdus de utilizator marit cu 2


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct lista{


int elem;
struct lista *next;
}list;

void adaugare(list *l){


int m;
while(l->next != NULL)
l = l->next;
printf("Introduceti urmatorul element:");
scanf("%d",&m);
l->next = malloc(sizeof(list));
l->next->elem = m+2;
l->next->next = NULL;
}

void print_list(list *l){


if(l != NULL){
printf("%d",l->elem);
print_list(l->next);
}
}
int main() {
list *l1;
int n;
l1 = malloc(sizeof(list));
printf("Introduceti primul element:");
scanf("%d",&n);
l1->elem = n+2;
l1->next = NULL;
adaugare(l1);
print_list(l1);
return 0;
}

Alta versiune a problemei anterioare:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct lista{


int elem;
struct lista *next;
}list;

void adaugare(list *l, int m){


while(l->next != NULL)
l = l->next;
l->next = malloc(sizeof(list));
l->next->elem = m;
l->next->next = NULL;
}

void print_list(list *l){


if(l != NULL){
printf("%d",l->elem);
print_list(l->next);
}
}
int main() {
list *l1;
int n;
l1 = malloc(sizeof(list));
printf("Introduceti primul element:");
scanf("%d",&n);
l1->elem = n;
l1->next = NULL;
adaugare(l1,n+1);
adaugare(l1,n+2);
print_list(l1);
return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main() {
int *v,n,i;
printf("Dati n:");
scanf("%d", &n);
v = (int*)malloc(sizeof(int)*n);
for(i = 0; i < n; i++){
printf("v[%d]=", i);
scanf("%d", &v[i]);
}
for(i = 0; i < n; i++){
printf("v[%d]=", i);
printf("%d", v[i]);
}
printf("Dupa realocare:");
v = (int*)realloc(v,(n+n)*sizeof(int));
for(i = 0; i < 2*n; i++){
printf("v[%d]=", i);
scanf("%d", &v[i]);
}
for(i = 0; i < 2*n; i++){
printf("v[%d]=", i);
printf("%d", v[i]);
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main() {
int *v,n,i;
v = (int*)malloc(sizeof(int)*4);
for(i = 0; i < 4; i++){
printf("v[%d]=", i);
scanf("%d", &v[i]);
}
for(i = 0; i < n; i++){
printf("v[%d]=", i);
printf("%d", v[i]);
}
printf("Dupa realocare:");
printf("Dati n:");
scanf("%d", &n);
v = (int*)realloc(v,n*sizeof(int));
for(i = 0; i < n; i++){
printf("v[%d]=", i);
scanf("%d", &v[i]);
}
for(i = 0; i < n; i++){
printf("v[%d]=", i);
printf("%d", v[i]);
}
return 0;
}

You might also like