#include <stdio.h>
int main (void)
{
int a[100], result;
int i; int y = 0;
while(scanf("%d", &i) == 1){
for (y=0; y< i; y++){
printf("#");
}
printf("\n");
}
return 0;
}
Wednesday, June 14, 2017
imgops
void normalize( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
uint8_t darkest = min(array, cols, rows);
uint8_t lightest = max(array, cols, rows);
uint8_t arr[lightest-darkest+1];
double scale_factor = (round) (256/(max-min+1));
for(int i=darkest; i<lightest-darkest+1; i++){
arr[i] = i;
}
for(int i=0; i<cols*rows; i++){
for(uint8_t k=0; k<lightest-darkest+1; i++){
if(arr[k] == array[i]){
double new = k*scale_factor;
array[i] = (uint8_t) new;
break;
}
}
}
}
unsigned int cols,
unsigned int rows )
{
uint8_t darkest = min(array, cols, rows);
uint8_t lightest = max(array, cols, rows);
uint8_t arr[lightest-darkest+1];
double scale_factor = (round) (256/(max-min+1));
for(int i=darkest; i<lightest-darkest+1; i++){
arr[i] = i;
}
for(int i=0; i<cols*rows; i++){
for(uint8_t k=0; k<lightest-darkest+1; i++){
if(arr[k] == array[i]){
double new = k*scale_factor;
array[i] = (uint8_t) new;
break;
}
}
}
}
Sunday, June 11, 2017
cmpt 125; assn 2; q2
#include<stdio.h>
void swap (int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void ssort_down(int arr[], int n)
{
int temp;
for(int i=n-1; i>0; i--){
int largest =i;
for(int j=i-1; j>=0; j--){
if(arr[j] > arr[largest]){
largest =j;
}
}
temp = arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
}
}
int max(int arr[], int start, int end)
{
int largest;
if(start == end){
return end;
}
largest = max(arr, start+1, end);
if(arr[start] > arr[largest]){
largest = start;
}
return largest;
}
void rec_ssort(int arr[], int n)
{
if(n==0){
return arr[0];
}
largest = rec_ssort(arr,n-1);
max(arr, 0, n)//wtf????
}
int main (void)
{
int arr[6] = {3,11,2,8,9,10};
int largest = max(arr,1,5);
/*for(int i=0; i<5; i++){
printf("%d", arr[i]);
}*/
printf("%d", largest);
return 0;
}
void swap (int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void ssort_down(int arr[], int n)
{
int temp;
for(int i=n-1; i>0; i--){
int largest =i;
for(int j=i-1; j>=0; j--){
if(arr[j] > arr[largest]){
largest =j;
}
}
temp = arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
}
}
int max(int arr[], int start, int end)
{
int largest;
if(start == end){
return end;
}
largest = max(arr, start+1, end);
if(arr[start] > arr[largest]){
largest = start;
}
return largest;
}
void rec_ssort(int arr[], int n)
{
if(n==0){
return arr[0];
}
largest = rec_ssort(arr,n-1);
max(arr, 0, n)//wtf????
}
int main (void)
{
int arr[6] = {3,11,2,8,9,10};
int largest = max(arr,1,5);
/*for(int i=0; i<5; i++){
printf("%d", arr[i]);
}*/
printf("%d", largest);
return 0;
}
cmpt 125 assn 2 q1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void sorted( int arr[], int len)
{
for(int i =0; i<len; i++){
arr[i] = i;
}
}
void reversed (int arr[], int len)
{
int element = len-1;
// numbers go backwards
for(int i =0; i<len; i++){
arr[i] = element;
element--;
}
}
void rando (int arr[], int len)
{
srand(time(0));
for(int i=0; i<len; i++){
arr[i] = rand();
}
}
void selectionSort( int arr[], int len )
{
int temp;
for( int i =0; i<len-1; i++){
int smallest = i;
for( int k=i+1; k<len; k++){
if(arr[k]< arr[smallest]){
smallest = k;
}
}
temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
}
}
void insertionSort( int arr[], int len)
{
int temp;
for( int i=0; i<len; i++){
temp = arr[i];
int pos = i;
while( pos >0 && arr[pos-1] > temp){
arr[pos] = arr[pos-1];
pos--;
}
arr[pos] = temp;
}
}
int main (void)
{
int array[80000];
reversed(array, 80000); //insert array here
// clock_t begin = clock(); //starts timing the sort
selectionSort(array, 80000); //array to sort
// clock_t end = clock();
// double duration = (double)(end-begin)/CLOCKS_PER_SEC;
// printf("%f", duration);
return 0;
}
#include<stdlib.h>
#include<time.h>
void sorted( int arr[], int len)
{
for(int i =0; i<len; i++){
arr[i] = i;
}
}
void reversed (int arr[], int len)
{
int element = len-1;
// numbers go backwards
for(int i =0; i<len; i++){
arr[i] = element;
element--;
}
}
void rando (int arr[], int len)
{
srand(time(0));
for(int i=0; i<len; i++){
arr[i] = rand();
}
}
void selectionSort( int arr[], int len )
{
int temp;
for( int i =0; i<len-1; i++){
int smallest = i;
for( int k=i+1; k<len; k++){
if(arr[k]< arr[smallest]){
smallest = k;
}
}
temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
}
}
void insertionSort( int arr[], int len)
{
int temp;
for( int i=0; i<len; i++){
temp = arr[i];
int pos = i;
while( pos >0 && arr[pos-1] > temp){
arr[pos] = arr[pos-1];
pos--;
}
arr[pos] = temp;
}
}
int main (void)
{
int array[80000];
reversed(array, 80000); //insert array here
// clock_t begin = clock(); //starts timing the sort
selectionSort(array, 80000); //array to sort
// clock_t end = clock();
// double duration = (double)(end-begin)/CLOCKS_PER_SEC;
// printf("%f", duration);
return 0;
}
Saturday, June 10, 2017
Lab 3: Tasks 4-5
/*
* imageops.c - Simple operations on images
*
* C laboratory exercises.
* Richard Vaughan, 2014.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*-------------------------------------------------
PART 0: DEMONSTRATION
*/
// get the value in the array at coordinate (x,y)
uint8_t get_pixel( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y )
{
assert( x < cols );
assert( y < rows );
return array[ y*cols + x ];
}
// set the pixel at coordinate {x,y} to the specified color
void set_pixel( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y,
uint8_t color )
{
assert( x < cols );
assert( y < rows );
array[ y * cols + x ] = color;
}
// Set every pixel to 0 (black)
void zero( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here.
}
// Returns a pointer to a freshly allocated array that contains the
// same values as the original array, or a null pointer if the
// allocation fails. The caller is responsible for freeing the array
// later.
uint8_t* copy( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 1: OPERATIONS ON THE WHOLE IMAGE
*/
/* TASK 1 - three easy functions to get started */
// Return the darkest color that appears in the array; i.e. the
// smallest value
uint8_t min( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int dark = array[0];
for(int i=1; i<cols*rows; i++){
if(array[i]<dark){
dark = array[i];
}
}
return dark;
}
// Return the lightest color that appears in the array; i.e. the
// largest value
uint8_t max( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int light = array[0];
for(int i =1; i<cols*rows; i++){
if(array[i]> light){
light = array[i];
}
}
return light;
}
// TASK 2
// Replace every instance of pre_color with post_color.
void replace_color( uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t pre_color,
uint8_t post_color )
{
for(int i=0; i<cols*rows; i++){
if(array[i] == pre_color){
array[i] = post_color;
}
}
}
/* TASK 3 - two functions */
// flip the image, left-to-right, like in a mirror.
void flip_horizontal( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start, end, temp;
for(int i=0; i<rows; i++){
start = i*cols;
end = (cols-1)+i*cols;
while(start<end){
temp= array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}
// flip the image top-to-bottom.
void flip_vertical( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start1, start2;
int startrow = 0, endrow= rows-1;
while(startrow<endrow){
start1=startrow*cols;
start2=endrow*cols;
for(int i =0; i<cols; i++){
temp = array[start1];
array[start1] = array[start2];
array[start2] = temp;
start1++;
start2++;
}
startrow++;
endrow--;
}
}
/* TASK 4 */
// Find the first coordinate of the first pixel with a value that
// equals color. Search from left-to-right, top-to-bottom. If it is
// found, store the coordinates in *x and *y and return 1. If it is
// not found, return 0.
int locate_color( const uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t color,
unsigned int *x,
unsigned int *y )
{
int found =0, index;
for(unsigned int yy =0; yy<rows; yy++){
for(unsigned int xx=0; xx<cols;xx++){
index = xx + yy*cols;
if( array[index] == color ){
*x = xx;
*y = yy;
found = 1;
break;
}
}
if( found == 1 ){
break;
}
}
return found;
}
/* TASK 5 */
// Invert the image, so that black becomes white and vice versa, and
// light shades of grey become the corresponding dark shade.
void invert( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
uint8_t greyscale[256];
int color = 255;
// create an array with numbers 0 to 255:
for(int i =0; i< 256; i++){
greyscale[i] = color;
color--;
}
// inverts the image:
for(int k=0; k<cols*rows; k++){
array[k] = greyscale[array[k]];
}
}
/* TASK 6 */
// Multiply every pixel by scale_factor, in order to brighten or
// darken the image. Any resulting value over 255 is
// thresholded to 255.
void scale_brightness( uint8_t array[],
unsigned int cols,
unsigned int rows,
double scale_factor )
{
//your code here
}
/* TASK 7 */
// Normalize the dynamic range of the image, i.e. Shift and scale the
// pixel colors so that that darkest pixel is 0 and the lightest pixel
// is 255. Hint: you already wrote min() and max().
void normalize( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 8 */
// Return a new image of size rows/2 by cols/2 If the original image
// has an odd number of columns, ignore its rightmost column. If the
// original image has an odd number of rows, ignore its bottom row.
// The value of a pixel at (p,q) in the new image is the average of
// the four pixels at (2p,2q), (2p+1,2q), (2p+1,2q+1), (2p,2q+1) in
// the original image.
uint8_t* half( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 2: OPERATIONS ON IMAGE SUB-REGIONS
These functions operate only on a rectangular region of the array
defined by a (left,top) corner (i.e. closer to the image origin) and
(right,bottom) corner (i.e. further from the image origin).
The region includes all the columns from [left, right-1] inclusive,
and allthe rows from [top, bottom-1] inclusive.
In every case, you may assume that left <= right and top <= bottom:
do not need to test for this.
The area of the region is right-left * bottom-top pixels, which
implies that if left == right or top == bottom, the region has no
area and is defined as "empty". Each function notes how to handle
empty regions.
In every function, use assert() to test bounds on the region
corners.
*/
/* TASK 9 */
// Set every pixel in the region to color. If the region is empty, the
// image must be unchanged.
void region_set( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
uint8_t color )
{
// your code here
}
/* TASK 10 */
// Return the sum of all the pixels in the region. Notice the large
// return type to handle potentially large numbers. Empty regions
// return the sum 0.
unsigned long int region_integrate( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return 0;
}
/* TASK 11 */
// Get a new image which is a copy of the region. Empty regions return
// a null pointer. If memory allocation fails, return a null
// pointer. The caller is responsible for freeing the returned array
// later.
uint8_t* region_copy( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return NULL;
}
* imageops.c - Simple operations on images
*
* C laboratory exercises.
* Richard Vaughan, 2014.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*-------------------------------------------------
PART 0: DEMONSTRATION
*/
// get the value in the array at coordinate (x,y)
uint8_t get_pixel( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y )
{
assert( x < cols );
assert( y < rows );
return array[ y*cols + x ];
}
// set the pixel at coordinate {x,y} to the specified color
void set_pixel( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y,
uint8_t color )
{
assert( x < cols );
assert( y < rows );
array[ y * cols + x ] = color;
}
// Set every pixel to 0 (black)
void zero( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here.
}
// Returns a pointer to a freshly allocated array that contains the
// same values as the original array, or a null pointer if the
// allocation fails. The caller is responsible for freeing the array
// later.
uint8_t* copy( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 1: OPERATIONS ON THE WHOLE IMAGE
*/
/* TASK 1 - three easy functions to get started */
// Return the darkest color that appears in the array; i.e. the
// smallest value
uint8_t min( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int dark = array[0];
for(int i=1; i<cols*rows; i++){
if(array[i]<dark){
dark = array[i];
}
}
return dark;
}
// Return the lightest color that appears in the array; i.e. the
// largest value
uint8_t max( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int light = array[0];
for(int i =1; i<cols*rows; i++){
if(array[i]> light){
light = array[i];
}
}
return light;
}
// TASK 2
// Replace every instance of pre_color with post_color.
void replace_color( uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t pre_color,
uint8_t post_color )
{
for(int i=0; i<cols*rows; i++){
if(array[i] == pre_color){
array[i] = post_color;
}
}
}
/* TASK 3 - two functions */
// flip the image, left-to-right, like in a mirror.
void flip_horizontal( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start, end, temp;
for(int i=0; i<rows; i++){
start = i*cols;
end = (cols-1)+i*cols;
while(start<end){
temp= array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}
// flip the image top-to-bottom.
void flip_vertical( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start1, start2;
int startrow = 0, endrow= rows-1;
while(startrow<endrow){
start1=startrow*cols;
start2=endrow*cols;
for(int i =0; i<cols; i++){
temp = array[start1];
array[start1] = array[start2];
array[start2] = temp;
start1++;
start2++;
}
startrow++;
endrow--;
}
}
/* TASK 4 */
// Find the first coordinate of the first pixel with a value that
// equals color. Search from left-to-right, top-to-bottom. If it is
// found, store the coordinates in *x and *y and return 1. If it is
// not found, return 0.
int locate_color( const uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t color,
unsigned int *x,
unsigned int *y )
{
int found =0, index;
for(unsigned int yy =0; yy<rows; yy++){
for(unsigned int xx=0; xx<cols;xx++){
index = xx + yy*cols;
if( array[index] == color ){
*x = xx;
*y = yy;
found = 1;
break;
}
}
if( found == 1 ){
break;
}
}
return found;
}
/* TASK 5 */
// Invert the image, so that black becomes white and vice versa, and
// light shades of grey become the corresponding dark shade.
void invert( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
uint8_t greyscale[256];
int color = 255;
// create an array with numbers 0 to 255:
for(int i =0; i< 256; i++){
greyscale[i] = color;
color--;
}
// inverts the image:
for(int k=0; k<cols*rows; k++){
array[k] = greyscale[array[k]];
}
}
/* TASK 6 */
// Multiply every pixel by scale_factor, in order to brighten or
// darken the image. Any resulting value over 255 is
// thresholded to 255.
void scale_brightness( uint8_t array[],
unsigned int cols,
unsigned int rows,
double scale_factor )
{
//your code here
}
/* TASK 7 */
// Normalize the dynamic range of the image, i.e. Shift and scale the
// pixel colors so that that darkest pixel is 0 and the lightest pixel
// is 255. Hint: you already wrote min() and max().
void normalize( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 8 */
// Return a new image of size rows/2 by cols/2 If the original image
// has an odd number of columns, ignore its rightmost column. If the
// original image has an odd number of rows, ignore its bottom row.
// The value of a pixel at (p,q) in the new image is the average of
// the four pixels at (2p,2q), (2p+1,2q), (2p+1,2q+1), (2p,2q+1) in
// the original image.
uint8_t* half( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 2: OPERATIONS ON IMAGE SUB-REGIONS
These functions operate only on a rectangular region of the array
defined by a (left,top) corner (i.e. closer to the image origin) and
(right,bottom) corner (i.e. further from the image origin).
The region includes all the columns from [left, right-1] inclusive,
and allthe rows from [top, bottom-1] inclusive.
In every case, you may assume that left <= right and top <= bottom:
do not need to test for this.
The area of the region is right-left * bottom-top pixels, which
implies that if left == right or top == bottom, the region has no
area and is defined as "empty". Each function notes how to handle
empty regions.
In every function, use assert() to test bounds on the region
corners.
*/
/* TASK 9 */
// Set every pixel in the region to color. If the region is empty, the
// image must be unchanged.
void region_set( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
uint8_t color )
{
// your code here
}
/* TASK 10 */
// Return the sum of all the pixels in the region. Notice the large
// return type to handle potentially large numbers. Empty regions
// return the sum 0.
unsigned long int region_integrate( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return 0;
}
/* TASK 11 */
// Get a new image which is a copy of the region. Empty regions return
// a null pointer. If memory allocation fails, return a null
// pointer. The caller is responsible for freeing the returned array
// later.
uint8_t* region_copy( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return NULL;
}
Friday, June 9, 2017
scrambled.c (done)
int scrambled( unsigned int a[], unsigned int b[], unsigned int len )
{
int smallest, j, temp;
int scramble = 1;
//sorts a[]:
for(int i =0; i<len-1; i++){
smallest = i;
for( int k=i+1; k<len; k++){
if(a[k] < a[smallest]){
smallest = k;
}
}
temp = a[i];
a[i]=a[smallest];
a[smallest] = temp;
}
//sorts b[]:
for(int i =0; i<len-1; i++){
smallest = i;
for( int k=i+1; k<len; k++){
if(b[k] < b[smallest]){
smallest = k;
}
}
temp = b[i];
b[i]=b[smallest];
b[smallest] = temp;
}
//compares a[] with b[]:
for(int i =0; i<len; i++){
if(a[i] != b[i]){
scramble = 0;
break;
}
}
return scramble;
}
{
int smallest, j, temp;
int scramble = 1;
//sorts a[]:
for(int i =0; i<len-1; i++){
smallest = i;
for( int k=i+1; k<len; k++){
if(a[k] < a[smallest]){
smallest = k;
}
}
temp = a[i];
a[i]=a[smallest];
a[smallest] = temp;
}
//sorts b[]:
for(int i =0; i<len-1; i++){
smallest = i;
for( int k=i+1; k<len; k++){
if(b[k] < b[smallest]){
smallest = k;
}
}
temp = b[i];
b[i]=b[smallest];
b[smallest] = temp;
}
//compares a[] with b[]:
for(int i =0; i<len; i++){
if(a[i] != b[i]){
scramble = 0;
break;
}
}
return scramble;
}
Lab 3: Tasks 1-3
/*
* imageops.c - Simple operations on images
*
* C laboratory exercises.
* Richard Vaughan, 2014.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*-------------------------------------------------
PART 0: DEMONSTRATION
*/
// get the value in the array at coordinate (x,y)
uint8_t get_pixel( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y )
{
assert( x < cols );
assert( y < rows );
return array[ y*cols + x ];
}
// set the pixel at coordinate {x,y} to the specified color
void set_pixel( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y,
uint8_t color )
{
assert( x < cols );
assert( y < rows );
array[ y * cols + x ] = color;
}
// Set every pixel to 0 (black)
void zero( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here.
}
// Returns a pointer to a freshly allocated array that contains the
// same values as the original array, or a null pointer if the
// allocation fails. The caller is responsible for freeing the array
// later.
uint8_t* copy( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 1: OPERATIONS ON THE WHOLE IMAGE
*/
/* TASK 1 - three easy functions to get started */
// Return the darkest color that appears in the array; i.e. the
// smallest value
uint8_t min( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int dark = array[0];
for(int i=1; i<cols*rows; i++){
if(array[i]<dark){
dark = array[i];
}
}
return dark;
}
// Return the lightest color that appears in the array; i.e. the
// largest value
uint8_t max( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int light = array[0];
for(int i =1; i<cols*rows; i++){
if(array[i]> light){
light = array[i];
}
}
return light;
}
// TASK 2
// Replace every instance of pre_color with post_color.
void replace_color( uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t pre_color,
uint8_t post_color )
{
for(int i=0; i<cols*rows; i++){
if(array[i] == pre_color){
array[i] = post_color;
}
}
}
/* TASK 3 - two functions */
// flip the image, left-to-right, like in a mirror.
void flip_horizontal( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start, end, temp;
for(int i=0; i<rows; i++){
start = i*cols;
end = (cols-1)+i*cols;
while(start<end){
temp= array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}
// flip the image top-to-bottom.
void flip_vertical( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start1, end1, start2, end2;
int startrow = 0, endrow= rows-1;
while(startrow<endrow){
start1=startrow*cols;
start2=endrow*cols;
//end1=(cols-1)+startrow*cols;
//end2=(cols-1)+endrow*cols;
for(int i =0; i<cols; i++){
temp = array[start1];
array[start1] = array[start2];
array[start2] = temp;
start1++;
start2++;
}
startrow++;
endrow--;
}
}
/* TASK 4 */
// Find the first coordinate of the first pixel with a value that
// equals color. Search from left-to-right, top-to-bottom. If it is
// found, store the coordinates in *x and *y and return 1. If it is
// not found, return 0.
int locate_color( const uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t color,
unsigned int *x,
unsigned int *y )
{
int found =0;
//your code here
for(int i =0; i<cols*rows; i++){
}
return found;
}
/* TASK 5 */
// Invert the image, so that black becomes white and vice versa, and
// light shades of grey become the corresponding dark shade.
void invert( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 6 */
// Multiply every pixel by scale_factor, in order to brighten or
// darken the image. Any resulting value over 255 is
// thresholded to 255.
void scale_brightness( uint8_t array[],
unsigned int cols,
unsigned int rows,
double scale_factor )
{
// your code here
}
/* TASK 7 */
// Normalize the dynamic range of the image, i.e. Shift and scale the
// pixel colors so that that darkest pixel is 0 and the lightest pixel
// is 255. Hint: you already wrote min() and max().
void normalize( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 8 */
// Return a new image of size rows/2 by cols/2 If the original image
// has an odd number of columns, ignore its rightmost column. If the
// original image has an odd number of rows, ignore its bottom row.
// The value of a pixel at (p,q) in the new image is the average of
// the four pixels at (2p,2q), (2p+1,2q), (2p+1,2q+1), (2p,2q+1) in
// the original image.
uint8_t* half( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 2: OPERATIONS ON IMAGE SUB-REGIONS
These functions operate only on a rectangular region of the array
defined by a (left,top) corner (i.e. closer to the image origin) and
(right,bottom) corner (i.e. further from the image origin).
The region includes all the columns from [left, right-1] inclusive,
and allthe rows from [top, bottom-1] inclusive.
In every case, you may assume that left <= right and top <= bottom:
do not need to test for this.
The area of the region is right-left * bottom-top pixels, which
implies that if left == right or top == bottom, the region has no
area and is defined as "empty". Each function notes how to handle
empty regions.
In every function, use assert() to test bounds on the region
corners.
*/
/* TASK 9 */
// Set every pixel in the region to color. If the region is empty, the
// image must be unchanged.
void region_set( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
uint8_t color )
{
// your code here
}
/* TASK 10 */
// Return the sum of all the pixels in the region. Notice the large
// return type to handle potentially large numbers. Empty regions
// return the sum 0.
unsigned long int region_integrate( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return 0;
}
/* TASK 11 */
// Get a new image which is a copy of the region. Empty regions return
// a null pointer. If memory allocation fails, return a null
// pointer. The caller is responsible for freeing the returned array
// later.
uint8_t* region_copy( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return NULL;
}
* imageops.c - Simple operations on images
*
* C laboratory exercises.
* Richard Vaughan, 2014.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*-------------------------------------------------
PART 0: DEMONSTRATION
*/
// get the value in the array at coordinate (x,y)
uint8_t get_pixel( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y )
{
assert( x < cols );
assert( y < rows );
return array[ y*cols + x ];
}
// set the pixel at coordinate {x,y} to the specified color
void set_pixel( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int x,
unsigned int y,
uint8_t color )
{
assert( x < cols );
assert( y < rows );
array[ y * cols + x ] = color;
}
// Set every pixel to 0 (black)
void zero( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here.
}
// Returns a pointer to a freshly allocated array that contains the
// same values as the original array, or a null pointer if the
// allocation fails. The caller is responsible for freeing the array
// later.
uint8_t* copy( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 1: OPERATIONS ON THE WHOLE IMAGE
*/
/* TASK 1 - three easy functions to get started */
// Return the darkest color that appears in the array; i.e. the
// smallest value
uint8_t min( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int dark = array[0];
for(int i=1; i<cols*rows; i++){
if(array[i]<dark){
dark = array[i];
}
}
return dark;
}
// Return the lightest color that appears in the array; i.e. the
// largest value
uint8_t max( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int light = array[0];
for(int i =1; i<cols*rows; i++){
if(array[i]> light){
light = array[i];
}
}
return light;
}
// TASK 2
// Replace every instance of pre_color with post_color.
void replace_color( uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t pre_color,
uint8_t post_color )
{
for(int i=0; i<cols*rows; i++){
if(array[i] == pre_color){
array[i] = post_color;
}
}
}
/* TASK 3 - two functions */
// flip the image, left-to-right, like in a mirror.
void flip_horizontal( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start, end, temp;
for(int i=0; i<rows; i++){
start = i*cols;
end = (cols-1)+i*cols;
while(start<end){
temp= array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}
// flip the image top-to-bottom.
void flip_vertical( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int start1, end1, start2, end2;
int startrow = 0, endrow= rows-1;
while(startrow<endrow){
start1=startrow*cols;
start2=endrow*cols;
//end1=(cols-1)+startrow*cols;
//end2=(cols-1)+endrow*cols;
for(int i =0; i<cols; i++){
temp = array[start1];
array[start1] = array[start2];
array[start2] = temp;
start1++;
start2++;
}
startrow++;
endrow--;
}
}
/* TASK 4 */
// Find the first coordinate of the first pixel with a value that
// equals color. Search from left-to-right, top-to-bottom. If it is
// found, store the coordinates in *x and *y and return 1. If it is
// not found, return 0.
int locate_color( const uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t color,
unsigned int *x,
unsigned int *y )
{
int found =0;
//your code here
for(int i =0; i<cols*rows; i++){
}
return found;
}
/* TASK 5 */
// Invert the image, so that black becomes white and vice versa, and
// light shades of grey become the corresponding dark shade.
void invert( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 6 */
// Multiply every pixel by scale_factor, in order to brighten or
// darken the image. Any resulting value over 255 is
// thresholded to 255.
void scale_brightness( uint8_t array[],
unsigned int cols,
unsigned int rows,
double scale_factor )
{
// your code here
}
/* TASK 7 */
// Normalize the dynamic range of the image, i.e. Shift and scale the
// pixel colors so that that darkest pixel is 0 and the lightest pixel
// is 255. Hint: you already wrote min() and max().
void normalize( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
}
/* TASK 8 */
// Return a new image of size rows/2 by cols/2 If the original image
// has an odd number of columns, ignore its rightmost column. If the
// original image has an odd number of rows, ignore its bottom row.
// The value of a pixel at (p,q) in the new image is the average of
// the four pixels at (2p,2q), (2p+1,2q), (2p+1,2q+1), (2p,2q+1) in
// the original image.
uint8_t* half( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
// your code here
return NULL;
}
/*-------------------------------------------------
PART 2: OPERATIONS ON IMAGE SUB-REGIONS
These functions operate only on a rectangular region of the array
defined by a (left,top) corner (i.e. closer to the image origin) and
(right,bottom) corner (i.e. further from the image origin).
The region includes all the columns from [left, right-1] inclusive,
and allthe rows from [top, bottom-1] inclusive.
In every case, you may assume that left <= right and top <= bottom:
do not need to test for this.
The area of the region is right-left * bottom-top pixels, which
implies that if left == right or top == bottom, the region has no
area and is defined as "empty". Each function notes how to handle
empty regions.
In every function, use assert() to test bounds on the region
corners.
*/
/* TASK 9 */
// Set every pixel in the region to color. If the region is empty, the
// image must be unchanged.
void region_set( uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
uint8_t color )
{
// your code here
}
/* TASK 10 */
// Return the sum of all the pixels in the region. Notice the large
// return type to handle potentially large numbers. Empty regions
// return the sum 0.
unsigned long int region_integrate( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return 0;
}
/* TASK 11 */
// Get a new image which is a copy of the region. Empty regions return
// a null pointer. If memory allocation fails, return a null
// pointer. The caller is responsible for freeing the returned array
// later.
uint8_t* region_copy( const uint8_t array[],
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom )
{
// your code here
return NULL;
}
Tuesday, June 6, 2017
inrect.c (done)
int InRectangle( float pt[2], float rect[4] )
{
int contained = 0;int result = 0;
if( (pt[0]>=rect[0]) && (pt[0]<=rect[2])){
contained++;
}
if( (pt[1]>=rect[1]) && (pt[1]<=rect[3]) ){
contained++;
}
if ( contained == 2 ){
result = 1;
}
return result;
}
{
int contained = 0;int result = 0;
if( (pt[0]>=rect[0]) && (pt[0]<=rect[2])){
contained++;
}
if( (pt[1]>=rect[1]) && (pt[1]<=rect[3]) ){
contained++;
}
if ( contained == 2 ){
result = 1;
}
return result;
}
verticalgraph.c
#include<stdio.h>
int main (void)
{
int arr[81];int len =0;int a, b, max, height, i=0;
while(a != EOF){
a = scanf("%d", &b);
if( a == EOF){
break;
}
// creates array of integers
arr[i] = b;
i++;
}
//finds the largest integer
height = arr[0];
for(int y =1; y < i; y++){
if( arr[y] > height ){
height = arr[y];
}
}
max = height;
// does the thing
for( int x =0; x< height; x++){
for( int k =0; k<i; k++){
if(arr[k] < max){
putchar(' ');
} else {
putchar('#');
}
}
putchar('\n');
max--;
}
return 0;
}
int main (void)
{
int arr[81];int len =0;int a, b, max, height, i=0;
while(a != EOF){
a = scanf("%d", &b);
if( a == EOF){
break;
}
// creates array of integers
arr[i] = b;
i++;
}
//finds the largest integer
height = arr[0];
for(int y =1; y < i; y++){
if( arr[y] > height ){
height = arr[y];
}
}
max = height;
// does the thing
for( int x =0; x< height; x++){
for( int k =0; k<i; k++){
if(arr[k] < max){
putchar(' ');
} else {
putchar('#');
}
}
putchar('\n');
max--;
}
return 0;
}
letterfreq.c (done)
#include<stdio.h>
int main (void)
{
int a; int arr[27]; int total =0; char letter; float freq;
// assigns the value of zero to the whole array
for(int k =1; k<27; k++){
arr[k] = 0;
}
while( a != EOF){
a = getchar();
if (a == EOF){
break;
}
// converts letters to lower case
if( (a >= 'A') && (a <= 'Z') ){
a = a + 32;
}
if ((a>= 'a') && (a<= 'z')){
total++;
int index = a-96;
arr[index]= arr[index]+1;
}
}
for (int x = 1;x<27; x++){
if (arr[x] != 0){
letter = (char)(x+96);
freq = (float)arr[x]/total;
printf( "%c %.4f\n", letter, freq);
}
}
return 0;
}
int main (void)
{
int a; int arr[27]; int total =0; char letter; float freq;
// assigns the value of zero to the whole array
for(int k =1; k<27; k++){
arr[k] = 0;
}
while( a != EOF){
a = getchar();
if (a == EOF){
break;
}
// converts letters to lower case
if( (a >= 'A') && (a <= 'Z') ){
a = a + 32;
}
if ((a>= 'a') && (a<= 'z')){
total++;
int index = a-96;
arr[index]= arr[index]+1;
}
}
for (int x = 1;x<27; x++){
if (arr[x] != 0){
letter = (char)(x+96);
freq = (float)arr[x]/total;
printf( "%c %.4f\n", letter, freq);
}
}
return 0;
}
censored.c (done)
#include<stdio.h>
#include<string.h>
int main( int argc, char* argv[] )
{
int a,k; int match =0; int lenCurrent; int diff = 0;
// argc = number of words +1
while(a != EOF){
a = getchar();
if( a == EOF){ break; }
// tries to see if a matches argv
diff = 0;
for( int i =1; i< argc; i++){
//if( censored == 1 ){
// break;
//}
match = 0;
if( a == argv[i][0] ){
printf("%c", (char)a);
// iterates through the text file to match the argv
for( int j=1; j != 0; j++ ){
a = getchar();
if( a == argv[i][j] ){
match++;
if(match == strlen(argv[i])-1){
printf("\b%s", "CENSORED");
break;
}
} else {
printf("%c", (char)a);
break;
}
}
} else {
// if the char does not equal to the one in the string
diff++;
}
}
if( diff == argc-1 ){
printf("%c", (char)a);
}
}
return 0;
}
#include<string.h>
int main( int argc, char* argv[] )
{
int a,k; int match =0; int lenCurrent; int diff = 0;
// argc = number of words +1
while(a != EOF){
a = getchar();
if( a == EOF){ break; }
// tries to see if a matches argv
diff = 0;
for( int i =1; i< argc; i++){
//if( censored == 1 ){
// break;
//}
match = 0;
if( a == argv[i][0] ){
printf("%c", (char)a);
// iterates through the text file to match the argv
for( int j=1; j != 0; j++ ){
a = getchar();
if( a == argv[i][j] ){
match++;
if(match == strlen(argv[i])-1){
printf("\b%s", "CENSORED");
break;
}
} else {
printf("%c", (char)a);
break;
}
}
} else {
// if the char does not equal to the one in the string
diff++;
}
}
if( diff == argc-1 ){
printf("%c", (char)a);
}
}
return 0;
}
Subscribe to:
Posts (Atom)
-
#include<stdio.h> #include<stdlib.h> #include"list.h" void list_sort(list_t* list) { }
-
At the end of CMPT 225 course, a student will be able to: Software Development: convert specifications into high-level design, apply sof...