I'm having some problems with this function. Its main purpose is to sorts the array of classes(below) in descending order by the number of sections.
Here is the array of classes:
CIS_CLASSES *pCls, *pLast;
CIS_CLASSES clsList[NUM_CLS] =
{
{"CIS 35A", 2, {61, 63}},
{"CIS 35B", 1, {62}},
{"CIS 41A", 3, {1, 2, 61}},
{"CIS 28", 1, {61}},
{"CIS 22C", 4, {3, 4, 61, 63}},
{"CIS 26B", 1, {61}},
{"CIS 22B", 8, {1, 2, 3, 4, 6, 61, 62, 63}},
{"CIS 29", 1, {61}},
{"CIS 22A", 8, {1, 3, 5, 6, 7, 8, 61, 63}},
};
I've also defined NUM_CLS using struct:
#define NUM_CLS 9
typedef struct
{
char course[10];
int noSections;
int sections[16];
int final;
} CIS_CLASSES;`
Here is the function :
void sortDescend(CIS_CLASSES list[], CIS_CLASSES *pLast);
{
CIS_CLASSES *pCurr;
CIS_CLASSES *pWalk;
for (pCurr = list- 1; pCurr < pLast; pCurr++)
{
CIS_CLASSES temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= 0 && temp.final > pLast->list[pWalk].final)
{
pLast->list[pWalk + 1] = pLast->list[pWalk];
pWalk--;
}
pLast->list[pWalk + 1] = temp;
}
}
}
When I try to run this code I get the error message: no member named 'list' in 'CIS_CLASSES'. But I don't understand why? and How can I make it run? Please let me know if I'm missing anything!
Looking at your structure I don't see an element list there:
typedef struct
{
char course[10];
int noSections;
int sections[16];
int final;
} CIS_CLASSES;
Therefore
pLast->list[pWalk].final
must fail.
Please try to do as the following.
void sortDescend(CIS_CLASSES pInput[], CIS_CLASSES **pOutput);
{
// pOutput should be declared in your main function as the following
// CIS_CLASSES **pOutput = (CIS_CLASSES **)malloc(NUM_CLS * sizeof(PVOID));
// copy pointer arrays of CIS_CLASSES list elements
for(int i=0; i<NUM_CLS; i++)
{
pOutput[i] = &pInput[i];
}
// bubble sort
for (int i=0; i<NUM_CLS-1, i++)
{
for (int j=i+1; j<NUM_CLS; j++)
{
CIS_CLASSES temp = *pCurr;
if(pOutput[i]->noSection < pOutput[j]->noSection)
{
pCurr = pOutput[i];
pOutput[i] = pOutput[j];
pOutput[j] = pCurr;
}
}
}
// You can access the descending sorted list item like pOutput[index]->course, ...
}
Thanks All!
I updated my code to this :
void sortDescend(CIS_CLASSES list[], CIS_CLASSES *pLast);
{
CIS_CLASSES *pCurr;
CIS_CLASSES *pWalk;
for (pCurr = list+1; pCurr < pLast; pCurr++)
{
CIS_CLASSES temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= 0 && strcmp(temp.course, pWalk->course) > 0)
{
*(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
}
}
Related
i need to create a power set of array lo[], array lo[] is {5, 6, 7}; it's for my laboratories, I don't know how to do this in general power set is 5, {5, 6} {5, 7} {5, 6, 7}, all combinations and another important thing, power of array. the number of power set combinations is the same as the power. Power of array is 2^(elements of array); 2^3 = 8
should be 8 combination.
output:
!((C/A) U (A/B)): {5, 6, 7}
power set: {5}, {5, 6}, {5, 6, 7}, {6, 7}.....
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = { 1,1,1,1,1,1,1,0,0,0 };
int b[10] = { 0,0,0,0,1,1,1,1,1,1 };
int c[10] = { 1,1,1,0,0,0,0,1,1,1 };
int c1[10];
int c2[10];
int c3[10];
int c4[10];
int temp[10], temp1[10], a2[10] = { 1, 2, 3, 4, 5 };
int k = 0;
for (int i = 0; i < 10; i++) {
c1[i] = c[i];
if (c[i] == a[i]) {
c1[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
c2[i] = a[i];
if (a[i] == b[i]) {
c2[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
if (c1[i] == 1 || c2[i] == 1) {
c3[i] = 1;
}
}
for (int i = 0; i < 10; i++) {
if (c3[i] == 0) {
c4[i] = 1;
}
else
c4[i] = 0;
} //// the main part of power set
int lo[3] = {0, 0, 0};
printf("!((C/A) U (A/B)): ");
for (int i = 0; i < 10; i++) {
if (c4[i] == 1) {
printf("%d ", i + 1);
lo[k] = (i + 1);
}
}
}
The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2
This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}
I wrote a program that sorts the values of an array. It has 5 arrays,
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
and an array of pointers that holds those 5 arrays,
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
I want to sort the values of each array, but when I pass the arrays to the sorting function
sortArrValues(&pArr[i]);
it sees the first index of each array (arr1,arr2...) as the element of that array (pArr[i]), so pArr[i] is (3,2,0,1,4).
But I want pArr[i] to be the full array it meant to be, so in the first iteration pArr[i] will be (3,9,6,7).
Notes: the first index of each array indicates the length of that array (not including that index).
The sorting will skip the first index.
There are two additional functions,they're not used (you can skip them).
Here's the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int** arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (*arr[i] > *arr[i+1])
{
tmp = *arr[i];
*arr[i] = *arr[i + 1];
*arr[i + 1] = tmp;
}
}
}
There is some errors in your code.
First, you don't need to send &pArr[i], pArr[i] is enough to order your arrays. then your void sortArrValues(int** arr) become void sortArrValues(int* arr) which is clearer to read.
Second, in your sortValues, you when you change a value, you should restart (I know that's not very optimized but if you would want to make something really faster, you should use a quick sort. with your code, { 4, 5, 6, 2, 1 } will become { 4 5 2 1 6 }.
So that's your code with the fixes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int* arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
for (int i = 0 ; i < LEN ; i++) {
for (int j = 0 ; j < pArr[i][0] + 1 ; j++)
printf("%d ", pArr[i][j]);
printf("\n");
}
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int* arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (arr[i] > arr[i+1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
i = 0;
}
}
}
I tried to modify your program to fix issues. Some of these issues are already highlighted by others. There were some there were not. Here is the working snippet. Let me know if you need more explanation on any modification. Note that I have only modified your sort function. The idea is not to give you the fastest sorting algorithm, but a working one.
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
}
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0, j = 0;
for (i = 0; i < (**arr) - 1; i++)
{
for(j=i+1; j < (**arr) - 1; j++) {
if ((*arr)[i+1] > (*arr)[j+1])
{
tmp = (*arr)[i+1];
(*arr)[i+1] = (*arr)[j + 1];
(*arr)[j+1] = tmp;
}
}
printf("%d ", (*arr)[i+1]);
}
printf("%d\n", (*arr)[i+1]);
}
#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (int i=0; i<sizeof(num1); i++) {
final[i] = num1[i] + num2[i];
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
So, I did the above without the pointers. But with pointers, here is my attempt: I'm still new to pointers, and I'm playing with different practice problems.
#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (; *num1 != '\0'; *num1++) {
*final = *num1 + *num2;
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
The following does the trick:
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5], c;
int *n1= num1, *n2=num2, *f=final;
for (; n1<&num1[5]; ) {
*f++ = *n1++ + *n2++;
}
for (c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
I have a problem to understand Kruskal Algorithm. Here is the code
#include <stdio.h>
#define MAX_VERTICLES 100
#define INF 1000
int parent[MAX_VERTICLES];
int num[MAX_VERTICLES];
void setInit(int n) {
int i;
for (i = 0; i < n; i++) {
parent[i] = -1;
num[i] = 1;
}
}
int setFind(int vertex) {
int p, s, i = -1;
for (i = vertex;(p = parent[i]) >= 0; i = p)
;
s = i;
for (i = vertex;(p = parent[i]) >= 0; i=p)
parent[i]=s;
return s;
}
void setUnion(int s1, int s2) {
if (num[s1] < num[s2]) {
parent[s1]=s2;
num[s2]+=num[s1];
}
else {
parent[s2] = s1;
num[s1] += num[s2];
}
}
typedef struct {
int key;
int u;
int v;
}element;
#define MAX_ELEMENT 100
typedef struct {
element heap[MAX_ELEMENT];
int heap_size;
}HeapType;
void init(HeapType *h) {
h->heap_size = 0;
}
void printHeap(HeapType *h) {
int i;
int level = 1;
printf("\n==========");
for (i = 1; i <= h->heap_size;i++) {
if (i = level) {
printf("\n");
level *= 2;
}
printf("\t%d", h->heap[i].key);
}
printf("\n==========");
}
void insertMinHeap(HeapType *h, element item) {
int i;
i = ++(h->heap_size);
while ((i != 1) && (item.key < h->heap[i / 2].key)){
h->heap[i] = h->heap[i / 2];
i /= 2;
}
h->heap[i] = item;
}
element deleteMinHeap(HeapType *h) {
int parent, child;
element item, temp;
item = h->heap[1];
temp = h->heap[(h->heap_size)--];
parent = 1;
child = 2;
while (child <= h->heap_size) {
if ((child < h->heap_size) && (h->heap[child].key > h->heap[child + 1].key))
child++;
if (temp.key <= h->heap[child].key) break;
h->heap[parent] = h->heap[child];
parent = child;
child *=2;
}
h->heap[parent] = temp;
return item;
}
void insertHeapEdge(HeapType *h, int u, int v, int weight) {
element e;
e.u = u;
e.v = v;
e.key = weight;
insertMinHeap(h, e);
}
void insertAllEdges(HeapType *h){
insertHeapEdge(h, 0, 1, 13);
insertHeapEdge(h, 1, 2, 36);
insertHeapEdge(h, 2, 3, 12);
insertHeapEdge(h, 2, 4, 28);
insertHeapEdge(h, 3, 5, 32);
insertHeapEdge(h, 4, 5, 14);
insertHeapEdge(h, 0, 5, 19);
insertHeapEdge(h, 0, 6, 23);
insertHeapEdge(h, 1, 6, 15);
insertHeapEdge(h, 5, 6, 20);
}
void kruskal(int n) {
int edge_accepted = 0;
HeapType h;
int uset, vset;
element e;
init(&h);
insertAllEdges(&h);
setInit(n);
while (edge_accepted<(n-1)){
e = deleteMinHeap(&h);
uset = setFind(e.u);
vset = setFind(e.v);
if (uset != vset) {
printf("(%d,%d) %d \n", e.u, e.v, e.key);
edge_accepted++;
setUnion(uset, vset);
}7
}
}
void main(){
kruskal(7);
getchar();
}
I cannot understand how setFind and setUnion functions work.(the other things are fine)
Somebody can explain the algorithms explicitly, please?
The algorithm by Kruskal (which aims at the generation of a minimum spanning tree) needs subroutines for finding the connected component for a given vertex and the possibility to merge connected components.
Apparently, parent[i] stores one single vertex which can be followed until no parent is possible; the node which is reached this way is the root of the connected component - this node can be found via setFind; num[i] represents the number of children defined by this relation. Thus, the connected components are represented implicity.
The function setUnion aims at merging the smaller connected component into the larger one by attaching the root of one connected component to the other component and updating the number of children.