Escolar Documentos
Profissional Documentos
Cultura Documentos
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;
return stud;
}
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;
}
}
char c;
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==',');
}
}
FILE *pf=fopen(argv[1],"r");
if (pf==NULL)
{
perror("No students!");
return -100;
}
int nrstuds=compute_nr_students(pf);
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);
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;
}
LAB 1 - Introduction
#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;
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
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()
{
int i = 0;
int pos_max = 0;
int length_max = 0;
int nr_of_words = 0;
int pos_tmp = i;
int word_length = 0;
pos_max = pos_tmp;
length_max = word_length;
}
nr_of_words++;
}
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);
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++)
{
int j;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 50
int main()
{
int a[N], b[N];
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 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");
}
*/
#include <stdio.h>
#include <stdlib.h>
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
int n = 0;
printf("n = ");
scanf("%d", &n);
int i;
for(i = 0; i< n; i++)
{
a[i] = (int*)malloc(n*sizeof(int));
}
int j;
scanf("%d", (*(a+i)+j));
}
}
calc_transposed(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 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);
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;
k = start[i];
start[i] = start[j];
start[j] = k;
k = end[i];
end[i] = end[j];
end[j] = k;
}
}
}
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()
{
int m, n;
}
}
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;
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';
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);
backtr(1);
struct car {
char brand[15];
char owner[15];
char color[10];
int nr;
};
int main()
{
int n;
printf("n = ");
scanf("%d", &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
printf("\n");
}
write(df, x, sizeof(x));
{
struct car tmp = x[i];
x[i] = x[j];
x[j] = tmp;
}
}
}
char col[10];
printf("\nGive me a color: ");
scanf("%s", col);
return 0;
}