September 29, 2017 | Author: RaduBeche | Category: Numbers, Computer Data, Arithmetic, Discrete Mathematics, Computer Architecture
Share Embed Donate

Short Description

colocviu asdn...


#include #include #include

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; } #include #include #include

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; i0) return -1; else if (dif 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 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 #include #include

#define N 100

int findsubstr(char*, char*);

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) || 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 #include #include

int main() { int n; printf("n = "); scanf("%d", &n);

char name[n][15]; //n strings with max length of 15

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 #include struct carry { char name[20]; int amount; int ppk; //price per kilo }; int main() { int m, n;

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
View more...


Copyright ©2017 KUPDF Inc.