How do I call functions within this code? Im using the visual studio software. keep getting errors that array1 and 2 are undefined - error-handling

enter image description here
Sorry im new to coding . I have searched up possible solutions for this on here but they didnt work. Im also confused on why some code appears grey compared to the rest.
https://1drv.ms/w/s!Ag8vVFKVPyOg6HeYLehGjQKdvl_3?e=QHY6t9
#include <stdio.h>
// initialised variables
int i = 0;
int count = 0;
void displayfunction(void);
int month = 0;
void highervalues(float array1[12], float array2[12]);
void highervalues(float array1[12], float array2[12]) {
for (i = 12; i > 0; i--) {
if (array2[i] > array1[i]) {
count = count + 1;
}
}
}
//Reading values for array1 and array2
void displayfunction(void) {
highervalues(array1[12] , array2[12]);
for (i = 0; i < 12; i++)
{
month = month + 1; // month increases by 1 after each input
printf_s("enter values for year 1 month %d", month);
scanf_s("%f", &array1[i]);
}
for (month = 12; month > 0; month--) {
}
for (i = 0; i < 12; i++) {
month = month + 1;
printf_s("enter values for year 2 month %d", month);
scanf_s("%f", &array2[i]);
}
}
/*comapring 2 arrays and increasing count value if there are any value in array2
are greater than array1*/
int main() {
displayfunction();
int array1[12];
int array2[12];
}

You have a fundamental misunderstanding of variable scope
int main() {
displayfunction();
int array1[12];
int array2[12];
}
Those arrays are only available in main. If you want other functions to operate on them you have to pass them as paramters to those functions. Plus they dont exist at the point where you try to call displayFunction
So change
void displayfunction(void)
to
void displayfunction(float array1[12], float array2[12])
then in main do
int main() {
int a1[12];
int a2[12];
displayfunction(a1, a2);
}
Note that I have changed the names here just to emphasise that its the not the fact that the names are the same thats important.

Related

Implementing qsort in Objective-C

I'm trying to use qsort to sort a C array in descending order based on what this website is suggesting.
Here is the relevant code:
int x = 3;
- (IBAction)CaptureButton:(id)sender
{
x++;
if (x % 3 == 1)
{
int areas[detectedBlobs.size()];
for (int i = 0; i < detectedBlobs.size(); i++)
{
areas[i] = detectedBlobs[i].getWidth() * detectedBlobs[i].getHeight();
}
int compareInts(void const *item1, void const *item2)
{ // first error
int const *int1 = item1;
int const *int2 = item2;
return (*int2 - *int1);
}
qsort(areas, detectedBlobs.size(), sizeof(int), compareInts); // second error
}
}
Here are the two errors I'm getting:
First error:
Function definition is not allowed here
Second error:
Use of undeclared identifier 'compareInts'
If I cannot define the comparator (compareInts) function here, where do I have to define it? Also, how can I get the qsort function to recognize the comparator?
Objective-C does not allow function definitions inside methods. Move compareInts outside of the method, and make it static to hide from other translation units:
static int compareInts(const void* item1, const void* item2) {
const int* int1 = (const int*)item1;
const int* int2 = (const int*)item2;
return (*int2 - *int1);
}
- (IBAction)CaptureButton:(id)sender {
x++;
if (x % 3 == 1) {
int areas[detectedBlobs.size()];
for (int i = 0; i < detectedBlobs.size(); i++) {
areas[i] = detectedBlobs[i].getWidth() * detectedBlobs[i].getHeight();
}
qsort(areas, detectedBlobs.size(), sizeof(int), compareInts);
}
}

Confusion about my QuickSort algorithm & Mergesort algorithm

I am currently conducting empirical studies to evaluate the run-time complexities of the quicksort, and mergesort algorithms. To do this I run a random number generator that stores whatever amount of numbers I specify in a binary file. The ranges of those numbers are from 1-1,000,000.I then run tests of each algorithm starting from 100,000 numbers, incrementing by 50,000 each time, until 1,000,000 numbers are sorted on the last run. So 20 tests each. I have successfully completed each algorithm but my results are kind of puzzingly. This is a graph showing my results.
I understand that quicksort has a worst case of O(n2) time, but typically O(n·lg(n)) time. Mergesort has Θ(n·lg(n)) time.
Also I would like to note that when I started the timer I just used clock() from time.h, and calculated the time elapsed. I started my timer one line of code before I called my sorting function.
What I dont understand is how my graph shows mergesort is always double the time, and reaching triple the time to sort numbers compared to quicksort.
My only thought is that for my mergesort algorithm every time I divide my array in half I use malloc to create a new integer array for each half. Of course this means a large amount of calls are made to malloc considering the number sizes I am sorting.
int* mergeSort(int* nums, int size){
int* left;
int* right;
int middle = size/2;
if(size <= 1)
return nums;
split(nums, size, &left, &right, middle);
//I dont understand why the code below wouldnt work in place of the split()
//when i run it, in main, nothing gets printed out. I guess i lose my pointer to the beginning of my array.
//left = nums;
//right = nums+middle;
left = mergeSort(left, middle);
right = mergeSort(right, size - middle);
merge(nums,left,right,middle,size - middle);
free(left);
free(right);
return nums;
}
void split(int* nums, int size, int** left, int** right, int middle){
int *lft = (int*) malloc ((sizeof(int) * middle));
int *rght = (int*) malloc ((sizeof(int) * size - middle));
int mid = middle;
int upMid = size - middle;
int i;
for(i=0; i < mid; i++)
lft[i] = nums[i];
for(i=0; i < upMid; i++)
rght[i] = nums[i+middle];
*left = lft;
*right = rght;
}
void merge(int* num, int* left, int* right, int sizeLeft, int sizeRight){
int i,j,k,n;
i=j=k=0;
n=sizeLeft + sizeRight;
while(k < n){
if(i< sizeLeft){
if(j<sizeRight){
insert(num,left,right,&i,&j,&k);
}
else{
append(num, left, sizeLeft, &i, &k);
}
}
else{
append(num,right,sizeRight,&j,&k);
}
}
}
void insert(int* num, int* left, int* right, int* i, int* j, int* k){
/*int i,j,k,n;*/
if(left[*i]<right[*j]){
num[*k] = left[*i];
(*i)++;
}
else{
num[*k] = right[*j];
(*j)++;
}
(*k)++;
}
void append(int* num, int* half, int sizeHalf, int* i, int* k){
while(*i < sizeHalf){
num[*k]= half[*i];
(*i)++; (*k)++;
}
}
I would greatly appreciate any feedback on this question of mine, and any advice on maybe making my mergesort function more efficient. Thanks!!
I have implemented a merge sort algorithm, you can have a look. I malloc a bak array at the beginning of mergeSort and every merge use the it afterwards.
#include <string>
#include <stdlib.h>
void _mergeSort(int *array, int *bakArray, int len) ;
void mergeSort(int *array, int len)
{
int *bak = (int *)malloc(sizeof(int)*len) ;
_mergeSort(array, bak, len) ;
free(bak) ;
}
void _mergeSort(int *array, int *bakArray, int len)
{
if (len >= 2) {
int leftLen = len/2 ;
_mergeSort(array, bakArray, leftLen) ;
_mergeSort(array+leftLen, bakArray, len-leftLen) ;
int *pa = array ;
int *pb = array+leftLen ;
int aIndex = 0 ;
int bIndex = 0 ;
while (aIndex < leftLen && bIndex < len-leftLen) {
int a = pa[aIndex] ;
int b = pb[bIndex] ;
if (a < b) {
bakArray[aIndex+bIndex] = a ;
++aIndex ;
} else if (a == b) {
bakArray[aIndex+bIndex] = a ;
bakArray[aIndex+bIndex+1] = a ;
++aIndex ;
++bIndex ;
} else {
bakArray[aIndex+bIndex] = b ;
++bIndex ;
}
}
if (aIndex < leftLen) {
memcpy(bakArray+aIndex+bIndex, pa+aIndex, sizeof(int)*(leftLen-aIndex)) ;
} else if (bIndex < len-leftLen) {
memcpy(bakArray+aIndex+bIndex, pb+bIndex, sizeof(int)*(len-leftLen-bIndex)) ;
}
memcpy(array, bakArray, sizeof(int)*len) ;
}
}
static const int MaxArraySize = 100 ;
int main()
{
srand(time(NULL)) ;
int array[MaxArraySize] ;
for (int i = 0 ; i < MaxArraySize; ++i) {
array[i] = rand() % 10000 ;
}
mergeSort(array, MaxArraySize) ;
for (int i = 0 ; i < MaxArraySize; ++i) {
printf("%d ", array[i]) ;
}
printf("\n") ;
return 0 ;
}

Platform::Collections::Vector sorting

In Metro Style apps sometimes we use Platform::Collections::Vector to hold elements used in a ListView.
How to sort a Platform::Collections::Vector?
I'm aware there are plenty of structures in std that can be sorted but I was wondering if there was some method for Platform::Collections::Vector other than writing your own sort function.
Actually something like the below should also work:
auto vec = ref new Platform::Collections::Vector<T^>();
std::sort(begin(vec), end(vec));
I didn't find any suitable answer so I used this workaround.
It's a simple quicksort over a Platform::Collections::Vector
void swap (Platform::Collections::Vector<T^>^ vec, int pos1, int pos2)
{
T^ tmp = vec->GetAt(pos1);
vec->SetAt(pos1, vec->GetAt(pos2));
vec->SetAt(pos2,tmp);
}
int compare (T^ c1, T^ c2)
{
int c = wcscmp (c1->Title->Data(),c2->Title->Data());
return -c;
}
int PartitionVec (int left, int right,
Platform::Collections::Vector<T^>^ vec)
{
int i,j;
i = left;
for (int j = left + 1; j <= right; ++j)
{
if (compare (vec->GetAt(j),vec->GetAt(left)) > 0)
{
++i;
swap (vec,i,j);
}
}
swap (vec,left,i);
return i;
}
void QuickSortVec (Platform::Collections::Vector<T^>^ vec,
int start, int end)
{
if (end > start)
{
int pivot_point;
pivot_point = PartitionVec (start, end, vec);
QuickSortVec (vec,start,pivot_point - 1);
QuickSortVec (vec, pivot_point + 1, end);
}
}

How to count the number of repetitions that while does?

I am doing a year of college and am having MUCH difficulty at a specific point ....
I need to make a code that reads the population of the U.S. and Brazil, knowing that the population of the U.S. is higher than in Brazil.
The issue is I need to count the number of the while loop, after all if 1 means 1 more year if it is 2 years older is 2 and so on. the code is here and is objective-c...
int pAmericana, pBrasileira, ano;
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSLog(#"What year is it?");
scanf("%i", &Ano);
NSLog(#"which the American people?");
scanf("%i", &pAmericana);
NSLog(#"which the Brazilian people?");
scanf("%i", &pBrasileira);
while (pAmericana >= pBrasileira) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
}
//FAILURE TO KNOW HOW TO COUNT HOW OFTEN HAPPENS THE LOOP .....
}
return 0;
}
You can use a counter inside the loop:
int i = 0;
while (pAmericana >= pBrasileira) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
i++;
}
NSLog(#"Loop count %i", i);
Adding a count variable to the while loop works. But you can also use a for loop:
int i = 0;
for (; pAmericana >= pBrasileira; i++) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
}
NSLog(#"Loop executed %d times", i);
You must declare the variable before the for loop so its value is available after the loop.
Note - you can change the calculations to:
int i = 0;
for (; pAmericana >= pBrasileira; i++) {
pAmericana *= 1.2;
pBrasileira *= 1.4;
}
int i = 0 before the while, i++; in the while loop, NSLog(#"Loops: %d", i); after the loop.
You can declare an integer variable outside the while loop then increment it within the loop, like this:
int i = 0; // Initialise the counter
while (pAmericana >= pBrasileira) {
...
i++; // Increment the counter
}

How do i create a calculated measure that will filter data by days overdue

I have a field in my fact table called days overdue. I would like to create a set that will do the following: If the days due is between 0 - 29, then 0 - 29 days overdue, if between 30 and 59 days old, then '30 - 59 days overdue. How would i create this?
We need to know what kind of array you're using, or linked list, or my favorite for these things, a vector, etc.
If you were using a vector, you would create your own class to be used as a datatype with things like:
Class MyData
{
String name;
int daysPastDue; // how you want to factor this is up to you,
// i suggest looking into Java.util.date or Java.util.calendar
public MyData
{
name = "";
daysPastDue = 0;
}
}
Class DoWork
{
public void myWork() // excuse the indent, forgot to put in the class name
{
vector <MyData> input;
MyData 0To29 [] = new MyData[input.size()];
MyData 33To59 [] = new MyData[input.size()];
MyData item = new MyData();
int 0To29count = 0;
int 30To59count = 0;
for (i = 0; i <= list.size(); i++)
{
item = input.elementAt(i)
if (item.daysPastDue <= 29)
{
0To29[0To29Count] = input;
0To29Count ++;
}
elseif (item.daysPastDue >= 30 && item.daysPastDue <= 59)
{
30To59[30To59Count] = input;
30To59Count ++;
}
}
}
}
then you have your 2 arrays and can output them as you wish. however i would recommend starting at daysPastDue = 100000 and decrement it and check the number through the vector until you have all the items in the vector listed. That way they're all in order from the most past due, to the least and you get the output of exactly how long they've been past due.