Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- double* getArray(int);
- double* addElement(double* grades, int size, double el, int newIndex);
- void printArr(double*, int);
- //double* removeElement(double* grades, int size, int index);
- double* removeElement(double* array, int *size, int index)
- {
- for(int i = 0; i < *size; i++)
- {
- if(i >= index)
- {
- array[i] = array[i+1];
- }
- }
- *size -= 1;
- double *newArray = (double*)realloc(array, *size * sizeof(double));
- if(newArray != array)
- free(array);
- return array;
- }
- void main()
- {
- int size = 5;
- double* grades = getArray(size);
- printArr(grades, size);
- printf("----Add-----\n");
- double newGrade = 1;
- while(newGrade != 0){
- printf("Vavedi nova ocenka (0 za krai): ");
- scanf("%lf", &newGrade);
- if(newGrade == 0){
- break;
- }
- int newIndex;
- printf("Vavedi index: ");
- scanf("%d", &newIndex);
- size++;
- grades = addElement(grades, size, newGrade, newIndex);
- }
- printf("----new-----\n");
- printArr(grades, size);
- printf("----Remove-----\n");
- double newIndex = 1;
- while(newIndex != -1){
- printf("Vavedi nova ocenka (-1 za krai): ");
- scanf("%lf", &newIndex);
- if(newIndex == -1){
- break;
- }
- grades = removeElement(grades, &size, newIndex);
- }
- printArr(grades, size);
- }
- /*double* removeElement(double* grades, int size, int index)
- {
- double* newArray = (double*)realloc(grades, size * sizeof(double));
- int currIndex = 0;
- int gradesIndex = 0;
- while(currIndex < size){
- if(currIndex == index){
- gradesIndex++;
- } else {
- newArray[currIndex] = grades[gradesIndex];
- currIndex++;
- gradesIndex++;
- }
- }
- return newArray;
- }*/
- double* addElement(double* grades, int size, double el, int newIndex)
- {
- double* newArray = (double*)realloc(grades, size * sizeof(double));
- double temp = newArray[newIndex];
- newArray[newIndex] = el;
- newArray[size-1] = temp;
- return newArray;
- }
- void printArr(double* grades, int size)
- {
- for (int i = 0; i < size; i++)
- {
- printf("Ocenka no. %d : %.2lf\n", i+1, grades[i]);
- }
- }
- double* getArray(size)
- {
- double* arr = (double*)malloc(size * sizeof(double));
- // double* arr = calloc(5, sizeof(double));
- for (int i = 0; i < size; i++)
- {
- printf("Vavedi ocenka no. %d: ", i+1);
- scanf("%lf", &arr[i]);
- }
- return arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement