Initial commit

This commit is contained in:
2023-05-22 23:28:51 -03:00
commit 5c1403aa91
467 changed files with 18649 additions and 0 deletions

5
Labs/Lab1/C/HelloWorld.c Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}

BIN
Labs/Lab1/C/HelloWorld.exe Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,5 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

BIN
Labs/Lab1/Lab1.pdf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}

View File

@ -0,0 +1,33 @@
#include <stdio.h>
int main(){
int value;
int iErr;
printf("Value to examine: ");
iErr = scanf("%d",&value);
if(iErr != 1){
printf("Unable to read the value\n");
return 0;
}
if(value <= 0) {
printf("Value must be positive\n");
return 0;
}
int binaryArray[32];
int arrayLength = 0;
while(value > 0) {
binaryArray[arrayLength] = value % 2;
value = value/2;
arrayLength++;
}
int counter = 0;
for(int i = arrayLength; i >= 0; i--) {
if (binaryArray[i] == 1) {
counter++;
}
}
printf("Number of ones: %d\n", counter);
return 0;
}

View File

@ -0,0 +1,5 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Binary file not shown.

33
Labs/Lab1/countOnes.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
int main(){
int value;
int iErr;
printf("Value to examine: ");
iErr = scanf("%d",&value);
if(iErr != 1){
printf("Unable to read the value\n");
return 0;
}
if(value <= 0) {
printf("Value must be positive\n");
return 0;
}
int binaryArray[32];
int arrayLength = 0;
while(value > 0) {
binaryArray[arrayLength] = value % 2;
value = value/2;
arrayLength++;
}
int counter = 0;
for(int i = arrayLength; i > 0; i--) {
if (binaryArray[i] == 1) {
counter++;
}
}
printf("Number of ones: %d", counter);
return 0;
}

BIN
Labs/Lab1/countOnes.exe Normal file

Binary file not shown.

BIN
Labs/Lab2/Lab2.pdf Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

26
Labs/Lab2/lab2.c Normal file
View File

@ -0,0 +1,26 @@
/* p1.c */
#include <stdio.h>
#include <stdlib.h>
int g1(int a, int b){
int c = (a + b) * b;
printf("g1: %d %d %d \n", a, b, c);
printf("a's address is %p, b's address is %p, c's address is %p\n", &a, &b, &c);
return c;
}
int g2(int a, int b){
int c = g1(a + 3, b - 11);
printf("g2: %d %d %d \n", a, b, c);
printf("a's address is %p, b's address is %p, c's address is %p\n", &a, &b, &c);
return c - b;
}
int main (int argc, char** argv){
int a = 5;
int b = 17;
int c = g2(a - 1, b * 2);
printf("main: %d %d %d \n", a, b, c);
printf("a's address is %p, b's address is %p, c's address is %p\n", &a, &b, &c);
return EXIT_SUCCESS;
}

BIN
Labs/Lab3/Lab3.pdf Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,30 @@
// arithmetic1.c
#include <stdio.h>
#include <stdlib.h>
int main (int argc ,char * * argv)
{
int arr1[] = {7, 2, 5, 3, 1, 6, -8, 16, 4};
char arr2[] = {'m', 'q', 'k', 'z', '%', '>'};
double arr3[] = {3.14, -2.718, 6.626, 0.529};
int len1 = sizeof(arr1) / sizeof(int);
int len2 = sizeof(arr2) / sizeof(char);
int len3 = sizeof(arr3) / sizeof(double);
printf("lengths = %d, %d, %d\n", len1, len2, len3);
int * iptr = arr1;
char * cptr = arr2;
double * dptr = arr3;
printf("values = %d, %c, %f\t\tpointers = %p, %p, %p\n", * iptr, * cptr, * dptr, iptr, cptr, dptr);
iptr ++;
cptr ++;
dptr ++;
printf("values = %d, %c, %f\t\tpointers = %p, %p, %p\n", * iptr, * cptr, * dptr, iptr, cptr, dptr);
iptr ++;
cptr ++;
dptr ++;
printf("values = %d, %c, %f\t\tpointers = %p, %p, %p\n", * iptr, * cptr, * dptr, iptr, cptr, dptr);
iptr ++;
cptr ++;
dptr ++;
printf("values = %d, %c, %f\t\tpointers = %p, %p, %p\n", * iptr, * cptr, * dptr, iptr, cptr, dptr);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
int main() {
int arr[] = {10, 11, 12, 13, 14, 15, 16};
int size = sizeof(arr) / sizeof(int);
int i;
int *ptr = arr;
for(i = 0; i < size; i++) {
printf("%d\t%d\t", i, arr[i]);
printf("%p\t%d\n", ptr, *ptr);
*ptr ++;
}
}

12
Labs/Lab3/src/arrindex.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
int arrindex(int * arrptr, int * arrval) {
return (*arrval - *arrptr);
}
int main(){
int arr[] = {10, 11, 12, 13, 14, 15, 16};
int len = sizeof(arr)/sizeof(arr[0]);
int i;
for(i = 0; i < len; i++) {
printf("%d\t%d\n", i, arrindex(&arr, &arr[i]));
}
}

View File

@ -0,0 +1,28 @@
/*
* wrongindex.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * * argv)
{
int x = -2;
int arr[] = {0, 1, 2, 3, 4};
int y = 15;
printf("& x = %p, & y = %p\n", & x, & y);
printf("& arr[0] = %p, & arr[4] = %p\n", & arr[0], & arr[4]);
printf("x = %d, y = %d\n", x, y);
printf("pointers: x = %p, y = %p, arr[0] = %p, arr[1] = %p, arr[2] = %p, arr[3] = %p, arr[4] = %p\n", &x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
arr[-1] = 7;
arr[5] = -23;
printf("x = %d, y = %d\n", x, y);
printf("pointers: x = %p, y = %p, arr[0] = %p, arr[1] = %p, arr[2] = %p, arr[3] = %p, arr[4] = %p\n", &x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
arr[6] = 108;
printf("x = %d, y = %d\n", x, y);
printf("pointers: x = %p, y = %p, arr[0] = %p, arr[1] = %p, arr[2] = %p, arr[3] = %p, arr[4] = %p\n", &x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
arr[7] = -353;
printf("x = %d, y = %d\n", x, y);
printf("pointers: x = %p, y = %p, arr[0] = %p, arr[1] = %p, arr[2] = %p, arr[3] = %p, arr[4] = %p\n", &x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
return EXIT_SUCCESS;
}

BIN
Labs/Lab4/Lab4.pdf Normal file

Binary file not shown.

Binary file not shown.

BIN
Labs/Lab4/Lec10src.zip Normal file

Binary file not shown.

View File

@ -0,0 +1,19 @@
#ifndef STRINGS_H
#define STRINGS_H
// a cover function for malloc()
// malloc and return memory for a string of stringsize characters
// return (char*)NULL on failure
char* mallocString(int stringsize);
// just a cover function for free()
void freeString(char* s);
// create a duplicate string of s
// return it
// return (char*)NULL on failure
// should call mallocString(), and then strcpy()
char* duplicateString(char* s);
#endif

View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]){
char** stringList;
// allocate an array of pointers to strings (each string is a char*)
stringList = (char**) malloc(sizeof(char*) * argc);
if(stringList == (char**)NULL){
fprintf(stderr,"Memory failure, terminating");
return EXIT_FAILURE;
}
for(int i=0; i<argc; i++){
// allocate the string
stringList[i] = (char*) malloc( sizeof(char)* (strlen(argv[i]) +1) );
if(stringList[i] == (char*)NULL){
fprintf(stderr,"Memory failure, terminating");
for(int j=0; j<i; i++){
free(stringList[j]);
}
free(stringList);
return EXIT_FAILURE;
}
// copy commandline arguement
strcpy(stringList[i], argv[i]);
}
// Did we do it? Can we print them out?
for(int i=0; i<argc; i++){
printf("%s\n", stringList[i]);
}
// Cleaning up after ourselves, from the end to the beginning
for(int i=0; i<argc; i++){
free(stringList[i]);
}
free(stringList);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#define MAXVALS 4
void arrayVals(int iN, int* arr, int val);
int main(void){
int iErr;
int iVal;
int a[MAXVALS];
char* prompt = "value: ";
printf("%s",prompt);
iErr = scanf("%d", &iVal);
if(iErr != 1){
fprintf(stderr, "Unable to read value\n");
return EXIT_FAILURE;
}
arrayVals(MAXVALS, a, iVal);
printf("-------------------------------------------\n");
printf("main prompt (char*):\t%p\n", &prompt);
printf("main a (int[MAXVALS]):\t%p\n", a);
printf("main iVal (int):\t%p\n", &iVal);
printf("main iErr (int):\t%p\n", &iErr);
for(int i=0; i<MAXVALS; i++)
printf("\t%d", a[i]);
printf("\n");
return EXIT_SUCCESS;
}
void arrayVals(int iN, int* arr, int val)
{
printf("arrayVals arr (int*):\t%p\n",&arr);
printf("arrayVals val (int):\t%p\n",&val);
printf("arrayVals iN (int):\t%p\n",&iN);
for(int i=0; i<iN; i++)
arr[i]= val;
}
/*
value: 3
arrayVals arr (int*): 0x7ffc99b277d0
arrayVals val (int): 0x7ffc99b277d8
arrayVals iN (int): 0x7ffc99b277dc
-------------------------------------------
main prompt (char*): 0x7ffc99b27808
main a (int[MAXVALS]): 0x7ffc99b27810
main iVal (int): 0x7ffc99b27824
main iErr (int): 0x7ffc99b27828
3 3 3 3
*/

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#define MAXVALS 4
int main(void){
// allocate space for a float on the heap,
// return a pointer to it,
// cast it to a float* ,
// store it in f;
float* pf = (float*) malloc( sizeof(float) );
if(pf == (float*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Please enter a float value: ");
scanf("%f",pf); // pf is a pointer, with space allocated to it
printf("%f it is!\n", *pf);
free(pf); // all done with fp
// allocate space for an integer [] that hold MAXVALS ints on the heap,
// return a pointer to it, cast the void* to a int* , and store it in arr;
int iErr;
int* arr = (int*) malloc( sizeof(int)*MAXVALS );
if(arr == (int*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Please enter %d integer values: ", MAXVALS);
for(int i=0; i<MAXVALS; i++){
iErr = scanf("%d",&arr[i]); // arr is a pointer, with space allocated
if(iErr != 1){
fprintf(stderr, "Read failed. Program terminating.");
free(arr);
return EXIT_FAILURE;
}
printf("%d it is!\n", arr[i]);
}
free(arr); //all done with arr
// allocate space for an char [] of that hold MAXVALS chars on the heap
// (with room for the NULL), return a pointer to it, cast it to a char* ,
// and store it in s.
// Note that although s isn't YET a string, that's the intention.
char* s = (char*) malloc( sizeof(char)*(MAXVALS+1) );
if(pf == (float*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Enter up to four characters: ");
getchar(); // flushes newline feed from last response
char c = getchar();
int i = 0;
while(c != '\n' && i < MAXVALS){ // leave room for the terminating NULL!
s[i] = c;
i++;
c = getchar();
}
s[i] = (char) NULL; // terminate the string
printf("%s it is!\n", s);
free(s); // all done with s
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include "Strings.h"
// copies the program name (argv[0]) to a new string, prints it out
int main(int argc, char* argv[]){
char* programName;
programName = duplicateString(argv[0]);
if(programName == (char*)NULL){
fprintf(stderr,"Memory failure, terminating");
return EXIT_FAILURE;
}
// Did we do it? Can we print it out?
printf("%s\n", programName);
free(programName);
return EXIT_SUCCESS;
}

BIN
Labs/Lab5/Lab5.pdf Normal file

Binary file not shown.