#include<stdio.h>
int scrambled( unsigned int a[], unsigned int b[], unsigned int len )
{
int arr[100], n=100;
int same = 1;
// changes all values in arr to 0
for(int i=0; i<n; i++){
arr[i] = 0;
}
// records the frequency of numbers in arr
for(int i=0; i<len; i++){
arr[a[i]]++;
}
// runs b over arr
for(int i=0; i<len; i++){
arr[b[i]]--;
}
//checks array over for -1 values
for(int i=0; i<n; i++){
if(arr[i] <= -1){
same = 0;
}
}
return same;
}
Showing posts with label Lab 2. Show all posts
Showing posts with label Lab 2. Show all posts
Sunday, June 18, 2017
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;
}
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;
}
Wednesday, May 31, 2017
count.c (done)
#include<stdio.h>
int main (void)
{
int a=0,b; int num; int o=0;
long unsigned int charcount = 0;
long unsigned int wordcount = 0;
long unsigned int linecount = 0;
while (a != EOF){
a = getchar();
if (!(a>='a' && a<='z') && !(a>='A' && a<='Z') && !(a == 39)){
wordcount++;
if (!(b>='a' && b<='z') && !(b>='A' && b<='Z') && !(b == 39)){
wordcount--;
}
}
if (a == '\n'){
linecount++;
}
charcount++;
b=a;
}
//wordcount =
printf( "%lu %lu %lu\n", charcount-1, wordcount, linecount );
return 0;
}
int main (void)
{
int a=0,b; int num; int o=0;
long unsigned int charcount = 0;
long unsigned int wordcount = 0;
long unsigned int linecount = 0;
while (a != EOF){
a = getchar();
if (!(a>='a' && a<='z') && !(a>='A' && a<='Z') && !(a == 39)){
wordcount++;
if (!(b>='a' && b<='z') && !(b>='A' && b<='Z') && !(b == 39)){
wordcount--;
}
}
if (a == '\n'){
linecount++;
}
charcount++;
b=a;
}
//wordcount =
printf( "%lu %lu %lu\n", charcount-1, wordcount, linecount );
return 0;
}
Tuesday, May 30, 2017
scrambled.c
#include<stdio.h>
int scrambled( unsigned int a[], unsigned int b[], unsigned int len )
{
int x, y;
unsigned int score1 = 0;
unsigned int score2 =0;
int scramble = 1;
if( len !=0){
for (x=0; x<len; x++){
for (y=0; y < len; y++){
if (b[y] == a[x]){
score1++;
break;
}
}
}
for (x=0; x<len; x++){
for (y=0; y < len; y++){
if (a[y] == b[x]){
score2++;
break;
}
}
}
}
if( (score1 != len) || (score2 != len) ){
scramble = 0;
}
return scramble;
}
int main( void )
{
unsigned int a[5] = {0,5,4,1,2} ;
unsigned int b[5] = {1,2,0,4,5};
if( scrambled( a, b, 5 ) == 1 )
{
printf( "b is a scrambled version of a\n" );
} else {
printf( "b has different contents to a\n" );
}
return 0;
}
int scrambled( unsigned int a[], unsigned int b[], unsigned int len )
{
int x, y;
unsigned int score1 = 0;
unsigned int score2 =0;
int scramble = 1;
if( len !=0){
for (x=0; x<len; x++){
for (y=0; y < len; y++){
if (b[y] == a[x]){
score1++;
break;
}
}
}
for (x=0; x<len; x++){
for (y=0; y < len; y++){
if (a[y] == b[x]){
score2++;
break;
}
}
}
}
if( (score1 != len) || (score2 != len) ){
scramble = 0;
}
return scramble;
}
int main( void )
{
unsigned int a[5] = {0,5,4,1,2} ;
unsigned int b[5] = {1,2,0,4,5};
if( scrambled( a, b, 5 ) == 1 )
{
printf( "b is a scrambled version of a\n" );
} else {
printf( "b has different contents to a\n" );
}
return 0;
}
Friday, May 26, 2017
contains.c (done)
#include<stdio.h>
#include<string.h>
int main (int argc, char* argv [])
{
int x, y, z;
int contained = 0;
int count =0;
int lenStr2 = 0; int i =0;
char str1[100];
strcpy(str1, argv[1]);
char str2[100];
strcpy(str2, argv[2]);
// counts the # of chars in Str2 - excludes null chracter
while (str2[i] != 0){
lenStr2++;
*(str2+i);
i++;
}
for(x=0; str1[x] != 0; x++){ //iterates through string 1
if (contained != 1){
if (str1[x] == str2[0]){ // if string1 == str2 first letter
for (y=0; str2[y] != 0; y++){ //iterates through str2
if ( str1[x] == str2[y]){
count++; //if letters match, add count
x++;
if( count == lenStr2){ //if the count equals to the len of str 2
contained = 1;
break;
}
}else{ //if the enxt chars dont match
break;
}
}
}
}else{
break;
}
}
// prints true or false statement
if (contained ==1){
printf("%s\n", "true");
} else {
printf("%s\n", "false");
}
return 0;
}
#include<string.h>
int main (int argc, char* argv [])
{
int x, y, z;
int contained = 0;
int count =0;
int lenStr2 = 0; int i =0;
char str1[100];
strcpy(str1, argv[1]);
char str2[100];
strcpy(str2, argv[2]);
// counts the # of chars in Str2 - excludes null chracter
while (str2[i] != 0){
lenStr2++;
*(str2+i);
i++;
}
for(x=0; str1[x] != 0; x++){ //iterates through string 1
if (contained != 1){
if (str1[x] == str2[0]){ // if string1 == str2 first letter
for (y=0; str2[y] != 0; y++){ //iterates through str2
if ( str1[x] == str2[y]){
count++; //if letters match, add count
x++;
if( count == lenStr2){ //if the count equals to the len of str 2
contained = 1;
break;
}
}else{ //if the enxt chars dont match
break;
}
}
}
}else{
break;
}
}
// prints true or false statement
if (contained ==1){
printf("%s\n", "true");
} else {
printf("%s\n", "false");
}
return 0;
}
Wednesday, May 24, 2017
L2 T3 done
void reverse( int arr[], unsigned int len )
{
int i;
int x =1;
int k = len;
int temp = arr[len];
for (k=len;k-1 > 0; k--)
{
for (i=0; i<len-x; i++){
temp = arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
x++;
}
}
{
int i;
int x =1;
int k = len;
int temp = arr[len];
for (k=len;k-1 > 0; k--)
{
for (i=0; i<len-x; i++){
temp = arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
x++;
}
}
Subscribe to:
Posts (Atom)
-
#include<stdio.h> #include<stdlib.h> #include"list.h" void list_sort(list_t* list) { }
-
#include<stdio.h> #include<math.h> int main (void){ int i, diff, xd, yd, hd, slope; //yd is useless int atZero =...