Example #1
0
int main(){
	int num = 0, i = 0, j = 0, size = 20;
	int randomNums[20] = {0}, count[101] = {0};
	int nums[20] = {0};
	srand((unsigned int) time(NULL));

	// TASK 1
	//task1();


	//TASK 2
	//task2(randomNums, 20, count);
	



	//TASK 3
	populateArray(nums, size);
	printArray(nums, size);
	sortArray(nums, size, 1);
	printf("\n");
	printArray(nums, size);
	printf("\n");
	sortArray(nums, size, 0);
	printArray(nums, size);



	return 0;
}
Example #2
0
int main()
{
	/* variable declarations */
	int amount, twenties, tens, fives, ones;
	int numbers [MAX], first, second;
	int *ptr;

	srand(time(NULL));

	/* prompt the user to enter a dollar amount as an integer and then read it in*/
	printf("Enter a dollaar amount (an integer): ");
	scanf("%d", &amount);

	/* invoke the function 'payAmount' to calculate the number of bills to return to the user
	pass 'amount' via pass-by-value, and all other variable via pass-by-reference (address)*/
	payAmount(amount, &twenties, &tens, &fives, &ones);

	/* print out the amount of bills */
	printf("\nYour equivalent amount of bills for %d dollars is:\n", amount);
	printf("$20 bills: %d\n", twenties);
	printf("$10 bills: %d\n", tens);
	printf("$5 bills: %d\n", fives);
	printf("$1 bills: %d\n", ones);

	/* just for fun assign the pointer 'ptr' to point to the variable
	'tens' print out its value. Then print out the address to which 'ptr'
	is pointint to and compare it to the address of 'tens'. */
	ptr = &tens;
	printf("Using a pointer, there are %d ten dollar bills\n", *ptr);
	printf("Printing the pointer's address to where it is pointing to %d and where 'tens' is located %d\n", ptr, &tens);

	/* invoke the function 'populateArray' and fill the array with random numbers */
	populateArray(numbers, MAX);

	/* invoke the 'printArray1' function to print out the array eleents in a normal fashion */
	printArray1(numbers, MAX);

	/* invoke the 'printArray2' function to print out the array eleents in a different fashion via pointers */
	printArray2(numbers, MAX);

	/* Randomly select two numbers that will represnet two subscripts for the array 'numbers' */
	first = rand() % MAX;
	second = rand() % MAX;

	/* invoke the funciton 'updateTwoElements' to modify the elements at position
	'first' and 'second' of array 'numbers' */
	updateTwoElements(numbers, first, second);

	/* print out the two elements of the array 'numbers' that were modified */
	printf("\nBack in function main\n");
	printf("Element %d is now %d\n", first, numbers[first]);
	printf("Element %d is now %d\n", second, numbers[second]);

	printf("\n\nThis program was written by Harry Staley");
	printf("\nEnd of program.\n");

    return 0; /* end program normally */
}/* end function Main*/
int main(int argc, char const *argv[]) {
	populateArray();
	int ans = 0, i = 2;
	for(; i < BOUND; i++)
		if(array[i] == array[i + 1])
			ans++;
	printf("%d\n", ans);
	return 0;
}
int main(void)
{
  const int nelem = 10;

  int *array;
  array = (int *)malloc(nelem*sizeof(int));

/*  Initialize the arrays  */
  for (indx = 0; indx < nelem; indx++)
  {
    array[indx] = 0;
  }

/* Replace initial values of array elements  */
  populateArray(nelem, array);

/* Print the elements of the unsorted array  */
  printf("The sequence of elements in the unsorted array are: [ ");

  for (indx = 0; indx < nelem; indx++)
  { 
    printf("%d ", array[indx]);
  }
  printf("]\n\n");

/* Sort the array of integers  */
  sortArray(nelem, array, ascending);

/* Print the elements of the sorted array  */
  printf("The sequence of elements in the sorted array are: [ ");
  for (indx = 0; indx < nelem; indx++)
  {
    printf("%d ", array[indx]);
  }
  printf("]\n\n");

/*  Compute average value of elements in array  */
  getAverage(nelem, array); 

/* Print sum of elements in array  */
  printf("The sum of the %d elements in the array is: %d\n\n ", nelem, sum);

/*  Print average */
/*  printf("The average value of the array elements is: %f\n\n ", getAverage(nelem, array));  */

  free(array);

  return 0;
}
Example #5
0
int main()
{
  srand(time(NULL));
  int intArray[ARRAY_SIZE];
  float floatArray[ARRAY_SIZE];
  
  populateArray(intArray);
  populateArray(floatArray);
  
  cout << "Your arrays BEFORE sorting: " << endl;
  outputArray(intArray);
  outputArray(floatArray);

  cout << endl;
  sort(intArray);
  sort(floatArray);
  
  cout << "Your arrays AFTER sorting: " << endl;
  outputArray(intArray);
  outputArray(floatArray);

  cout << "\nThat's all, folks!" << endl;
  return 0;
}
Example #6
0
int main(int argc, char *argv[]) {
	int current = getCurrentDesktop();
	int specified = getSpecifiedValue(argv, current);

    int wNum = getWindowsCountOfSpecifiedDesktop(specified + '0');

    char **arr = populateArray(specified + '0', wNum);
    char **commandsToExecute;

    printf("current: %d, specified: %d\n", current, specified);

    for(int i = 0; i < wNum; i++) {
    	printf("%s\n", arr[i]);
    	//printf("wmctrl -i -r %s -e 0,x,x,x,x\n", arr[i]);
    	//getXCommand("xwininfo -id %s | grep 'Absolute upper-left X:' | grep -Eo '(-?[0-9]+)'", arr[i]);
    	//printf("%d\n", getParameter("xwininfo -id %s | grep 'Absolute upper-left X:' | grep -Eo '(-?[0-9]+)'", arr[i]));
    	getParameters(arr[i]);
    }

    // int a = stringToInt("429");
    // printf("%d\n", a);
	// FILE *command = popen("wmctrl -d | wc -l", "r");
 //    printf("%d\n", countStringSize(command));
}
Example #7
0
int main()
{
	populateArray(bubbleSort);
	return 0;
}
Example #8
0
int main()
{

	//changed

	std::string input;
	std::stringstream ss1;
	int numSims;
	bool prompt = false;

	//get the number of courses to generate, put it in numSims,
	//and make sure the user enters a number
	do{
		std::cout << "Enter the number of courses to generate: ";
	}while(!getInt(&numSims,'\n'));
	

	//only ask certain questions if more than 3 courses are being made
	if(numSims > 3){prompt = true;}

	//these hold one value for each simulation
	int allPlanes [numSims]; //holds the number of planes for each sim
	int allWayPoints[numSims];	//the number of waypoints for each sim
	int allFields [numSims];	//length of the square field for each sim

	//these will potentially hold values that are common for all simulations
	//if they exists
	int nPlanes = -1;		
	int nWayPoints = -1;
	int nFields = -1;

	//other
	int count = 0;	//iteration variable
	char yn = '\0';	//for response to a yes no question

	
	//see if there is a number of planes for all sims, otherwise prompt for all values
	if(prompt && needOne("planes",&nPlanes)){}//do nothing else
	else{
		std::cout << "Enter number of planes for each simulation separated by a space:\n\n ";
		while(count<numSims-1){
			//std::cin >> allPlanes[count++];
			getInt(&(allPlanes[count++]),' ');
		}
		//get the last one, delim on the newline
		getInt(&(allPlanes[count]),'\n');
		count = 0;
	}
	//std::cin.clear();


	//same thing as with the planes but with waypoints
	if(prompt && needOne("wayPoints",&nWayPoints)){}
	else{
		std::cout << "Enter number of wayPoints for each plane in each simulation separated by a space:\n ";
		std::cout << "(The number of wayPoints in a given simulation is constant fo every plane.)\n";
		while(count<numSims-1){
			getInt(&(allWayPoints[count++]),' ');
		}
		getInt(&(allWayPoints[count++]),'\n');
		count = 0;
	}

	

	//same thing with field length
	if(prompt && needOne("field length",&nFields)){}
	else{
		std::cout << "Enter length of the field for each simulation separated by a space:\n\n ";
		while(count<numSims-1){
			getInt(&(allFields[count++]),' ');
		}
		getInt(&(allFields[count++]),'\n');
		count = 0;
	}

	std::string fn;
	std::cout << "\nEnter the base name for all files: ";
	//std::cout << "(the number of planes, and size of field will be added to the name, as well as a counter)\n";
	std::getline(std::cin,fn);
	
	fileName = fn.c_str();


	char autoPathName [1000];
	sprintf(autoPathName,"%s/%s%s",OUTPUT_DIRECTORY,fileName,AUTO_EXTENSION);
	//sprintf(autoPathName,"%s/%s%s",(ros::package::getPath("AU_UAV_ROS")+"/courses/"+autoFile+AUTOEXTENSION).c_str());
	autoFP = fopen(autoPathName,"w");



	//if a common value was entered, put that value in each position of the array
	if(nPlanes > 0){
		populateArray(allPlanes,nPlanes,numSims);
	}

	if(nWayPoints > 0){
		populateArray(allWayPoints,nWayPoints,numSims);
	}

	if(nFields > 0){
		populateArray(allFields,nFields,numSims);
	}


	//these are the predefined constants
	std::cout << "\nSettings:\n";
	std::cout << "OUTPUT DIRECTORY: " << OUTPUT_DIRECTORY <<"\n";
	std::cout << "Northwest Corner: ("<<NORTH_MOST_LATITUDE<<","<<WEST_MOST_LONGITUDE<<")\n";
	std::cout << "Plane buffer space: " << BUFFER_SPACE <<"\n";
	std::cout << "Generating...";


	//generate all the course files
	count =0;
	while(count<numSims){
		generateCourse(allPlanes[count],allWayPoints[count],allFields[count],count);
		count = count + 1;
	}


	fclose(autoFP);

}//end of main
Example #9
0
static void populateVars(Context ctx,const char *key,void *value,int type,int elems){
    int ret,ncgid,cnt;
    DimRef dr;
    double *pdDepth=NULL;
    char *pstrSource=NULL;
    size_t dep,*basei=NULL,*endi=NULL;
    size_t i=0;
    if(ctx.ncid<=0)return;
    if(type==LUA_TTABLE){//any other type at this level will be disgarded
	if(NC_NOERR!=(ret=nc_def_grp(ctx.ncid,key,&ncgid)))
	    nc_error(ret,__LINE__);
	ctx.ncid=ncgid;
	ctx.pfunCallback=populateAttrs;
	loopTable(&ctx,NULL,0);
	if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_FEATURE_TYPE_INSTANCE,1,&(dr.dim1[0]))))
	    nc_error(ret,__LINE__);
	if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_TIME,NC_UNLIMITED,&(dr.dim2[1]))))
	    nc_error(ret,__LINE__);
	pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_Z,DEPTH_LEVEL);
	if(pdDepth){
	    dep=(size_t)*pdDepth;
            if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_Z,dep,&(dr.dim3[2]))))
	        nc_error(ret,__LINE__);
	    free(pdDepth);
	}
	dr.dim2[0]=dr.dim3[0]=dr.dim1[0];
	dr.dim1t[0]=dr.dim3[1]=dr.dim2[1];
	dr.dim1z[0]=dr.dim3[2];
	ctx.pfunCallback=defineVars;
	ctx.param=(void*)&dr;
	loopTable(&ctx,LUA_VARS_ROOT,0);
	pstrSource=(char*)queryTableValue(ctx.L,LUA_VARS_ROOT,NULL,DATA_SOURCE);
	if(pstrSource&&dep>0){
	    pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,OBS_COUNT);
	    if(dep>1){
		basei=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MIN);
		endi=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MAX);
	    }
	    if(pdDepth){//dpDepth represents tscount here
		cnt=(int)(*pdDepth)*dep;
		free(pdDepth);
		ctx.obs=(double*)malloc(sizeof(double)*cnt);
		ctx.time=(double*)malloc(sizeof(double)*cnt);
		FILE *f=fopen(pstrSource,"r");
    		if(f){
		    double f1,f2,f3;
		    int ret=2;
		    if(1==dep){
		        while(!feof(f)&&ret==2&&cnt--){
			    ret=fscanf(f,"%lf,%lf",&f1,&f2);
	    		    ctx.time[i]=f1;
			    ctx.obs[i++]=f2;
			}
		    }else{
			if(basei&&endi&&*endi>=*basei){
			    double *pdKey=(double*)malloc(sizeof(double)*(*pdDepth));
			    ctx.height=(double*)malloc(sizeof(double)*cnt);
			    if(populateArray(ctx.L,VAR_Z,DEPTHS,LUA_TNUMBER,ctx.height,cnt)){
				qsort(ctx.height,dep,sizeof(double),compareDoubles);
				for(;i<cnt;++i)
				    *(ctx.obs+i)=ctx.missingval;
			        while(!feof(f)&&ret==2&&cnt--){
			            ret=fscanf(f,"%lf,%lf,%lf",&f1,&f2,&f3);
				    bsearch(f3,ctx.height,dep,sizeof(double),compareDoubles);
	    		            ctx.time[i]=f1;
			            ctx.obs[i++]=f2;
			        }
			    }
			    else{
				free(ctx.height,dep,sizeof(double),);
			  	ctx.height=NULL;
			    }
			    if(pdKey)free(pdKey);
			}
			else{
			    free(ctx.obs);
			    free(ctx.time);
			    ctx.obs=ctx.time=NULL;
			}
			
		    }
		    fclose(f);
		    ctx.pfunCallback=putVars;
		    ctx.len=i;
	            loopTable(&ctx,LUA_VARS_ROOT,0);
    		}
		if(basei)free(basei);
		if(endi)free(endi);
		if(ctx.height)free(ctx.height);
		if(ctx.time)free(ctx.time);
		if(ctx.obs)free(ctx.obs);
		free(pstrSource);
	    }
Example #10
0
int main()
{
	//Array to hold the numbers
	int numbers[500];

	//Holds the length of the array
	int numbersLength = sizeof(numbers) / sizeof(numbers[0]);

	/*-------------------QUICKSORT PART---------------------*/
	std::cout << "Creating the array for Quicksort" << std::endl;

	//Add the numbers to the array
	populateArray(numbers, numbersLength);
	
	std::cout << "Starting Quicksort" << std::endl;

	//Run quicksort while timing it
	auto time1 = std::chrono::high_resolution_clock::now();
	QuickSort::quickSort(numbers, 0, numbersLength - 1);
	auto time2 = std::chrono::high_resolution_clock::now();

	//Print the sorted numbers
	std::cout << "-------------------QUICKSORT PART---------------------" << std::endl;
	for (int i = 0; i < numbersLength; i++)
	{
		std::cout << numbers[i] << " ";
	}

	//Print the time elapsed to the screen
	std::cout << "\n\n" << "Quicksort took: " << std::chrono::duration_cast<std::chrono::milliseconds>(time2 - time1).count() << " milliseconds\n\n";







	/*-------------------MERGESORT PART---------------------*/
	std::cout << "Creating the array for Mergesort" << std::endl;

	//Replace the sorted array with random numbers
	populateArray(numbers, numbersLength);

	//Run the algorithm and time it
	time1 = std::chrono::high_resolution_clock::now();
	MergeSort::mergeSort(numbers, 0, numbersLength - 1);
	time2 = std::chrono::high_resolution_clock::now();

	//Print the sorted numbers
	std::cout << "-------------------MERGESORT PART---------------------" << std::endl;
	for (int i = 0; i < numbersLength; i++)
	{
		std::cout << numbers[i] << " ";
	}

	//Print the time elapsed to the screen
	std::cout << "\n\n" << "Mergesort took: " << std::chrono::duration_cast<std::chrono::milliseconds>(time2 - time1).count() << " milliseconds\n";

	//Pause the system so the results stay on screen
	system("pause");
	return 0;
}