//znalezione w sieci
#include <stdio.h>
#include <stdlib.h>
#define SIZE_X 4
#define SIZE_Y 3
struct Point
{
double value;
int x;
int y;
struct Point *next;
};
void addElement(struct Point **headPointer, double matrixValue, int matrixX, int matrixY)
{
struct Point *pointer;
pointer = *headPointer;
if (matrixValue != 0)
{
if(pointer == NULL)
{
pointer = (struct Point*)malloc(sizeof(struct Point));
pointer->value = matrixValue;
pointer->x = matrixX;
pointer->y = matrixY;
pointer->next = NULL;
*headPointer = pointer;
}
else
{
while (pointer->next != NULL)
{
pointer = pointer->next;
}
pointer->next = (struct Point*)malloc(sizeof(struct Point));
pointer = pointer->next;
pointer->value = matrixValue;
pointer->x = matrixX;
pointer->y = matrixY;
pointer->next = NULL;
}
}
}
void showAll(struct Point *head)
{
if (head != NULL)
{
printf("%lf (%d,%d)\n", head->value, head->x, head->y);
while(head->next != NULL)
{
head = head->next;
printf("%lf (%d,%d)\n", head->value, head->x, head->y);
}
}
}
void findMaxMin(struct Point *head, struct Point **max, struct Point **min)
{
if (head != NULL)
{
*max = head;
*min = head;
while (head->next != NULL)
{
head = head->next;
if (head->value > (*max)->value)
{
*max = head;
}
if (head->value < (*min)->value)
{
*min = head;
}
}
}
}
int main()
{
int i, j;
double matrix[SIZE_X][SIZE_Y];
struct Point *head, *maxElement, *minElement;
head = NULL;
maxElement = NULL;
minElement = NULL;
// inicjalizacja macierzy
for(i = 0; i < SIZE_X; i++)
{
printf("Wprowadz elementy %d wiersza (%d):", (i+1), SIZE_Y);
for(j = 0; j < SIZE_Y; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
// dodawanie elemntow macierzy rzadkiej
for(i = 0; i < SIZE_X; i++)
{
for(j = 0; j < SIZE_Y; j++)
{
addElement(&head, matrix[i][j], (i+1), (j+1));
}
}
showAll(head);
findMaxMin(head, &maxElement, &minElement);
printf("\n\nMin: %lf (%d,%d)\n", minElement->value, minElement->x, minElement->y);
printf("Max: %lf (%d,%d)\n", maxElement->value, maxElement->x, maxElement->y);
return 0;
}
Comments
0 B
|👍
/👎