#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *pf;
pf = fopen("myfile.txt","r+"); //w = write, r = read, a = append, r+ = open for both read and write
if (pf == NULL) {
perror("Cannot open file. Lollll."); return -100;
}
int n;
fseek(pf,0,SEEK_SET); fscanf(pf,"%d",&n);
fprintf(pf,"unguru %d",n);//afiseaza in fisier return 0; } http://users.utcluj.ro/~igiosan/Private/Recap/ http://users.utcluj.ro/~igiosan/Private/Recap_sol/ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int id;
char first[21],last[21]; float avg; int nrmarks; } student; typedef struct { float upperlimit; int nrstud; } range; int compute_nr_students(FILE *pf) { fseek(pf,0,SEEK_SET); int stud=0; char line[500]; while (fgets(line,sizeof(line),pf)!=NULL) stud++; return stud; }
void read_students(FILE *pf,student *s,int nrstuds) {
fseek(pf,0,SEEK_SET); int i;
for(i=0; i<nrstuds; i++) {
fscanf(pf,"%d/",&(s[i].id)); char fl[53];
fgets(fl,sizeof(fl),pf); char *p=strtok(fl,";"); strcpy(s[i].last,p); p=strtok(NULL,"\n"); strcpy(s[i].first,p); s[i].avg=0; s[i].nrmarks=0; } }
void display_all_students(FILE *pf,student *s, int nrstuds) {
fseek(pf,0,SEEK_SET); int i;
for(i=0; i<nrstuds; i++)
printf("%9d %-20s %-20s %5.2f\n",s[i].id,s[i].first,s[i].last,s[i].avg);
}
void compute_average_marks(FILE *pf,student *s,int nrstuds) {
int id;
while (fscanf(pf,"%d/",&id)==1) {
int i;
for (i=0; i<nrstuds; i++) if (s[i].id==id)
break;
int mark; do { fscanf(pf,"%d%c",&mark,&c); s[i].avg=(s[i].avg*s[i].nrmarks+mark)/(s[i].nrmarks+1); (s[i].nrmarks)++; } while (c==','); } }
int compare_students(const void *a, const void *b) {
double dif= ((student*)a)->avg-((student*)b)->avg; if (dif>0) return -1; else if (dif<0) return 1; return 0; }
range find_best_range(student *s, int nrstud) {
range r; r.nrstud=0; int i;
for (i=0; i<nrstud; i++) {
int count=0; int j;
for (j=i; j<nrstud; j++) if (s[i].avg-s[j].avg<=1) count++; else break; if (count>r.nrstud) { r.nrstud=count; r.upperlimit=s[i].avg; } } return r; }
int main(int argc, char* argv[]) { if (argc!=3) { perror("Incorrect nr. of arguments!"); return -1; } FILE *pf=fopen(argv[1],"r"); if (pf==NULL) { perror("No students!"); return -100; } int nrstuds=compute_nr_students(pf);
printf("There are %d students!\n\n",nrstuds);
student * s=(student *)malloc(nrstuds*(sizeof(student)));
read_students(pf,s,nrstuds); pf=fopen(argv[2],"r"); if (pf==NULL) { perror("No marks!"); return -200; } compute_average_marks(pf,s,nrstuds); qsort(s,nrstuds,sizeof(student),compare_students);
printf("Students table (average marks descending order):\n");
printf("%9s %-20s %-20s %5s\n","ID","FIRST_NAME","LAST_NAME","AVG"); printf("---\n");
display_all_students(pf,s,nrstuds);
range r=find_best_range(s,nrstuds);
printf("\nOne point interval with maximum number of students (highest marks):\n %d students in the range of [%5.2f, %5.2f]\n",r.nrstud,r.upperlimit-1,r.upperlimit);
fclose(pf); free(s);
return 0; }
#include <stdio.h> #include <stdlib.h> #include <conio.h> int main() { char s[200];
printf("\nPlease input a character string and press ENTER\n"); gets(s);//citeste sir/numar
printf("\nThe character string is\n"); puts(s);//afiseaza sir/numar
return 0; }
Citirea cu scanf c Character (char)
s Character string (char *) d Decimal integer
o Octal integer
x, X Hexadecimal integer u Unsigned integer
f Floating point (real) number ld, lo, lx, lX Long lu Unsigned long lf/Lf Double/long double #include <stdio.h> int main( )
{ int a; float b, c;
printf("\nPlease input the integer value of a="); scanf("%5d",&a);
printf("\nPlease input the value of the real number b="); scanf("%5f",&b);
c=a+b;
printf("\nthe value of the sum c=a+b is: %6.2f\n",c);// 6 si 2 sunt cifrele dinainte si dupa virgula
return 0; }
/* Computes of the roots of the equation a*x^2 +b*x +c =0 */ #include <stdio.h>
#include <math.h> int main()
{
float a, b, c, delta, x1, x2;
printf("\nComputes of the roots of the equation\n\ta*x^2 +b*x +c =0\n"); printf("Please input values for a, b, and c\n");
scanf("%f %f %f", &a, &b, &c); if (a!=0) { delta=b*b-4*a*c; if (delta >= 0) { x1=(-b-sqrt(delta))/(2*a); x2=(-b+sqrt(delta))/(2*a);
printf("\nThe equation has the roots x1=%g and x2=%g\n", x1, x2); }
else {
x1=-b/(2*a);
x2=sqrt(-delta)/(2*a);
printf("\n\nThe equation has complex roots x1=%g-j*%g\ and x2=%g+j*%g\n", x1, x2, x1, x2);
} }
else printf("\nEquation is not of second order (i.e. a=0)\n"); return 0;
}
/* Computes the greatest common divisor (gcd) and smallest common multiple (smc)
Of two natural numbers, a and b */ #include <stdio.h>
int main() {
int a, b, a1, b1, gcd, smc, remainder;
printf("Computes of the greatest common divisor (gcd) and\n");
printf( "the smallest commom multiple (scm) of two natural numbers, a and b"); printf("Input a value for a=");
scanf("%d",&a);
printf("Input a value for b="); scanf("%d",&b); /* Find the gcd */ a1=a; b1=b; do{ remainder=a1%b1; a1=b1;
b1=remainder; } while (remainder != 0); gcd=a1; smc=a*b/gcd; clrscr(); printf("a=%d b=%d gcd(a,b)=%d smc=%d", a, b, gcd, smc); return 0; }
//cauta un numar intr-un sir ordonat !!! #include <stdio.h> #include <stdlib.h> int main() { printf("n = "); int n; scanf("%d", &n); float a[100]; int i = 0; for (i = 0; i < n; i++) { printf("a[%d] = ", i); scanf("%f", &a[i]); } float x; printf("x = "); scanf("%f", &x); if (x > a[n-1])
{
printf("Number not found"); return 0;
} else {
for (i = 0; i<n; i++) {
if (x == a[i]) {
printf("Found %g at pos %d",x,i); break;
}
if (a[i]>x) {
printf("Number not found."); break; } } } return 0; }
//Given a sequence of n integer numbers, extract the maximum length subsequence
which is in ascending order. #include <stdio.h>
#include <stdlib.h>
int main() {
int a[100]; int i,n; printf("Number of elements: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("a[%d] = ", i); scanf("%d", &a[i]); } int length_max = 0; int pos_max = 0; int length = 0; int pos = 0; i = 0; while (i < n - 1) { if (a[i] < a[i+1]) {
length = 0; //length of subsequence pos = i; //position of subsequence start
while (i < n - 1 && a[i] < a[i+1]) {
length++; i++;
} if (length > length_max) { length_max = length; pos_max = pos; } } else i++; }
printf("The longest ascending subsequence starts at position %d. \nThe subsequence is: ", pos_max);
for (i = pos_max; i <= pos_max + length_max; i++) {
printf("%5d", a[i]); }
return 0; }
//Read a sentence from the standard input. Compute the number of the words and find and
display the longest one. #include <stdio.h>
#include <stdlib.h> #include <string.h>
int main() {
char s[255] = ""; printf("Give me a sentence: \n"); gets(s); int i = 0; int pos_max = 0; int length_max = 0; int nr_of_words = 0;
//find position of spaces in sentence while (i < strlen(s)) { while (s[i]==' ') { i++; } int pos_tmp = i; int word_length = 0;
while (s[i]!=' ' && i < strlen(s)) { word_length++; i++; } if (word_length > length_max) {
pos_max = pos_tmp;
length_max = word_length; }
nr_of_words++; }
printf("Number of words in the sentence: %d \n", nr_of_words); printf("Longest word starts at position %d. \n", pos_max); printf("Longest word is: ");
for (i = pos_max; i < pos_max + length_max; i++) {
printf("%c", s[i]); }
return 0; }
// Write a function to check whether a character string is a substring of another character
string. The function should return the position at which the substring starts if true, or –1 otherwise #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100
int main() { char a[N], b[N]; printf("String: "); gets(a); printf("Substring: "); gets(b);
int pos = findsubstr(b,a);
if (pos==-1) {
printf("\"%s\" was not found in \"%s\". \n", b, a); }
else {
printf("\"%s\" was found at position %d in \"%s\". \n", b, pos, a); }
return 0; }
//int findsubstr(char sub[], char s[]) >> ez is jo de egy kicsit jobban megfog //int findsubstr(char sub[10], char s[20]) >>> ez is jo, csak nagyon megfog
int findsubstr(char *sub, char *s) //ez nem fog meg char-tol tombig minden mehet {
int i;
for (i=0; i <= strlen(s) - strlen(sub); i++) {
for (j = 0; j < strlen(sub); j++) { if (s[i+j]!=sub[j]) { break; } } if (j==strlen(sub)) return i; } return -1; }
//Write a function to check if its parameter (positive integer) is a perfect square. Then apply
this function to a vector of positive integers, and extract all perfect squares and place them in
another vector.
#include <stdio.h> #include <stdlib.h> #include <math.h>
#define N 50
void readarray(int[], int*); void printarray(int*, int);
void extractperfectsquares(int[], int, int[], int*);
int main() {
int na, nb;
readarray(a, &na);
extractperfectsquares(a, na, b, &nb); printarray(b, nb);
return 0; }
int isperfectsquare(int x) {
int r = (int)(sqrt(x)+0.1); //kicsit hozza kell adni, mert megtortenhet, hogy sqrt(4) = 1.9999999999
if (x==r*r) return 1; return 0; }
void readarray(int x[], int *nx) {
printf("n = ");
scanf("%d", nx); //nx a cime int i;
for (i = 0; i < *nx; i++) //ez a tartlama a cimnek {
printf("x[%d] = ", i);
scanf("%d", &x[i]); // x[i] cimebe ird bele. }
void printarray(int *x, int nx) //x helyen nem tudjuk, hogy tomb van vagy csak egy sima szam { int i; for (i = 0; i < nx; i++) { printf("%d ", x[i]); } printf("\n"); }
void extractperfectsquares(int a[], int na, int b[], int *nb) {
*nb = 0; int i;
for (i = 0; i < na; i++) if(isperfectsquare(a[i])) { b[*nb] = a[i]; (*nb)++; } }
/ * 3.11. Write a function to calculate the transposed of a matrix, using pointers and pointer expressions
* to access to the elements of the matrix. The resulted matrices will be displayed in natural form.
*/
#include <stdio.h> #include <stdlib.h>
void calc_transposed(int** a, int n) { int i,j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i < j) { int tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp; } } } }
void print_matrix(int** a, int n) { int i,j; printf("\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++)
{ printf("%d ", a[i][j]); } printf("\n"); } printf("\n"); } int main() { int n = 0; printf("n = "); scanf("%d", &n);
int** a = (int**)malloc(n*sizeof(int*)); //egy 10 pointerbol allo tomb
int i;
for(i = 0; i< n; i++) { a[i] = (int*)malloc(n*sizeof(int)); } int j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("a[%d, %d] = ", i,j);
scanf("%d", (*(a+i)+j)); } } printf("\nOriginal matrix: "); print_matrix(a,n); calc_transposed(a,n); printf("\nTransposed matrix: "); print_matrix(a,n); return 0; }
//Write a function to delete a substring from a given character string, specifying the beginning
position and the length of the substring.
#include <stdio.h> #include <stdlib.h> #include <string.h>
int deletestr(char *s, int pos, int len) {
if (pos < 0 || pos > strlen(s) || len < 0) return 0; len = (len > strlen(s) - pos) ? strlen(s)-pos:len; strcpy(s+pos, s+pos+len);
return 1; }
int main() { char p[100]; gets(p); if (deletestr(p, 5, 3)) { printf("\n OKKK: %s \n", p); } else {
printf("FAIL, string remained: %s", p); }
return 0; }
//Read from the keyboard the names of n kings and the corresponding year limits of their state
leading periods. Display the name of all the kings in alphabetic order, and the number of years they
ruled. #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n; printf("n = "); scanf("%d", &n);
int start[n], end[n]; //start date and end date of reign
printf("Enter king names and dates like this: ex. Edward 1234-1253\n"); int i;
for (i = 0; i < n; i++) {
printf("%d: ", i);
scanf("%s %d-%d", name[i], &start[i], &end[i]); } char t[15]; int j, k; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (strcmp(name[i], name[j]) > 0) { strcpy(t, name[i]); strcpy(name[i], name[j]); strcpy(name[j], t); k = start[i]; start[i] = start[j]; start[j] = k; k = end[i]; end[i] = end[j]; end[j] = k;
} } }
printf("\nKings in alphabetic order and the corresponding years.\n");
for(i = 0; i < n; i++) {
printf("%10s ruled %3d years between %4d and %4d.\n", name[i], end[i] - start[i], start[i], end[i]);
}
return 0; }
/* 3.9.A lorry can carry at most m kilograms. The name of the materials, the amounts in kilograms, and
the price per kilo are known. Find a load composition such a way the value of the load is
maximum. */ #include <stdio.h> #include <stdlib.h> struct carry { char name[20]; int amount;
int ppk; //price per kilo };
int main() {
printf("Give me the capacity of the lorry in kg: "); scanf("%d", &m);
printf("Give me the number of materials: "); scanf("%d", &n);
struct carry x[n]; int i;
for(i=0; i < n; i++) {
printf("Name of %d. material: ", i+1); scanf("%s", x[i].name);
printf("Amount of %d. material: ", i+1); scanf("%d", &x[i].amount);
printf("PPK of %d. material: ", i+1); scanf("%d", &x[i].ppk);
} int j;
//sort in descending order depending on ppk for (i=0; i< n-1; i++)
{
for (j = i+1; j<n; j++) {
if (x[i].ppk < x[j].ppk) {
struct carry tmp = x[i]; x[i] = x[j];
x[j]= tmp; }
} } i = 0;
int sum = 0, last_amount = 0; while (i < n && sum < m) {
if (sum + x[i].amount <= m) {
sum = sum +x[i].amount; } else { last_amount = m - sum; sum = m; } i++; } int total_value = 0; for (j = 0; j < i - 1; j++) {
printf("The lorry carries %d kgs of %s.\n", x[j].amount, x[j].name); total_value += x[j].amount*x[j].ppk;
}
if (last_amount == 0) {
printf("The lorry carries %d kgs of %s.\n", x[i-1].amount, x[i-1].name); total_value += x[i-1].amount*x[i-1].ppk;
} else
{
printf("The lorry carries %d kgs of %s.\n", last_amount, x[i-1].name); total_value += last_amount*x[i-1].ppk;
}
printf("Total amount of carry is %d kgs, and total value is %d.", sum, total_value); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> char a[11][21]; int n, k; int x[11]; int nrvmax = 0; char smax[201]; int nrofvowels(char* x) { int i = 0, v = 0; while (i < strlen(x)) {
if(x[i]=='a' || x[i]=='e' || x[i]=='i' || x[i]=='o' || x[i]=='u') v++;
i = i + 2; }
return v; }
int valid(int i) { int k; for (k = 1; k < i; k++) { if(x[i] == x[k]) return 0; } return 1; } void printsol() { int i,j = 0; char constring[201] = ""; for (i = 1; i <= k; i++) { strcat(constring, a[x[i]]); j+= strlen(a[x[i]]); } constring[j] = '\0'; if (nrofvowels(constring) > nrvmax) { nrvmax = nrofvowels(constring); strcpy(smax, constring); } }
void backtr(int i) {
int j;
for (j = 1; j <= n; j++) // n = set size {
x[i]=j; if (valid(i)) {
if (i < n) //solution vector size = n backtr(i+1); else printsol(); } } } int main() { scanf("%d ", &k); char s[201]; gets(s); char* ps = strtok(s, " "); n = 0; while (ps!=NULL) { //puts(ps); n++; strcpy(a[n], ps); ps = strtok(NULL, " ");
}
backtr(1);
printf("%d %s", nrvmax, smax); return 0;
}
/ Write a program to accomplish the following actions:
- Create a file with data concerning cars in a garage. A car is represented by a
structure containing its brand name, owner name, color and number (from the
number plate).
- Display on the screen an alphabetic list of all cars of a given color, ordered by owner’s name. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include<sys/stat.h> #include <fcntl.h> struct car { char brand[15]; char owner[15]; char color[10]; int nr; }; int main() {
int n; printf("n = "); scanf("%d", &n); struct car x[n]; int i,j; for (i = 0; i < n; i++) {
printf("x[%d].brand = ", i); scanf("%15s", x[i].brand); printf("x[%d].owner = ", i); scanf("%15s", x[i].owner); printf("x[%d].color = ", i); scanf("%10s", x[i].color); printf("x[%d].nr = ", i); scanf("%d", &x[i].nr); printf("\n");
}
int df = open("file.dat", O_CREAT|O_RDWR|O_BINARY); if (df == -1)
{
perror("Cannot open file"); return -1; } write(df, x, sizeof(x)); for (i = 0; i < n-1;i++) { for (j = i+1; j < n; j++) { if (strcmp(x[i].owner, x[j].owner) > 0)
{
struct car tmp = x[i]; x[i] = x[j]; x[j] = tmp; } } } char col[10]; printf("\nGive me a color: "); scanf("%s", col); for (i = 0; i < n; i++) { if (strcmp(col, x[i].color) == 0) {
printf("Car brand: %s\nOwner: %s\nColor: %s\nNumber plate: %d\n\n", x[i].brand, x[i].owner, x[i].color, x[i].nr);
} }
return 0; }