Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROBLEM #1
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- struct CAR{
- char brand[20];
- int vin;
- };
- void loadArray(struct CAR[], int);
- void printArray(struct CAR[], int);
- int main(){
- srand(time(NULL));
- int arrayLength = 4;
- struct CAR newCar[arrayLength];
- loadArray(newCar, arrayLength);
- printArray(newCar, arrayLength);
- return 0;
- }
- void loadArray(struct CAR ary[], int length){
- for (int i = 0; i < length; i++){
- printf("Enter in a brand name for car %d: ", i + 1);
- scanf("%s", ary[i].brand);
- ary[i].vin = rand() % 9000 + 1000;
- }
- printf("\n");
- return;
- }
- void printArray(struct CAR ary[], int length){
- for (int i = 0; i < length; i++){
- printf("The brand name of car %d is %s.\n", i + 1, ary[i].brand);
- printf("The vehicle ID number of car %d is %d. \n", i + 1, ary[i].vin);
- }
- return;
- }
- PROBLEM #1 Challenge (Only loadArray needs to be modified):
- void loadArray(CAR ary[], int length){
- for (int i = 0; i < length; i++){
- printf("Enter in a brand name for car %d: ", i + 1);
- scanf("%s", ary[i].brand);
- ary[i].vin = rand() % 9000 + 1000;
- for (int j = 0; j < length; j++){
- if (ary[i].vin == ary[j].vin){
- ary[i].vin = rand() % 9000 + 1000;
- }
- }
- }
- printf("\n");
- return;
- }
- PROBLEM #2
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- typedef struct CAR{
- char brand[20];
- int vin;
- } CAR;
- void loadArray(CAR[], int);
- void printArray(CAR[], int);
- int main(){
- srand(time(NULL));
- int arrayLength = 4;
- CAR newCar[arrayLength];
- loadArray(newCar, arrayLength);
- printArray(newCar, arrayLength);
- return 0;
- }
- void loadArray(CAR ary[], int length){
- for (int i = 0; i < length; i++){
- printf("Enter in a brand name for car %d: ", i + 1);
- scanf("%s", ary[i].brand);
- ary[i].vin = rand() % 9000 + 1000;
- }
- printf("\n");
- return;
- }
- void printArray(CAR ary[], int length){
- for (int i = 0; i < length; i++){
- printf("The brand name of car %d is %s.\n", i + 1, ary[i].brand);
- printf("The vehicle ID number of car %d is %d. \n", i + 1, ary[i].vin);
- }
- return;
- }
- PROBLEM #3
- #include <stdio.h>
- #include <math.h>
- typedef struct{
- int x;
- int y;
- int res;
- } SAMPLE;
- void PowerUp(SAMPLE*);
- void PowerUpWrong(SAMPLE);
- int main(void) {
- SAMPLE sample1 = {2, 5, 0}; //Initializes fields/members
- SAMPLE* samplePtr;
- samplePtr = &sample1; //Create a pointer to the struct
- PowerUpWrong(sample1);
- printf("The incorrect result is %d\n", sample1.res);
- PowerUp(samplePtr);
- printf("%d raised to the power of %d is: %d\n", sample1.x, samplePtr->y, sample1.res);
- //Note the use of both the indirect and direct selection operators
- return 0;
- }
- void PowerUpWrong(SAMPLE ptr){
- int result = 0;
- ptr.res = (int)pow(ptr.x, ptr.y);
- return;
- }
- void PowerUp(SAMPLE* smp){
- int result = 0;
- result = (int)pow(smp->x, smp->y);
- smp->res = result;
- return;
- }
- PROBLEM #4
- #include <stdio.h>
- #define N 4 //Size of vectors
- typedef double SCALAR;
- typedef SCALAR VECTOR[N];
- SCALAR Dot_Product(VECTOR x, VECTOR y) {
- int i;
- SCALAR sum = 0.0;
- for (i = 0; i < N; i++) {
- sum += x[i] * y[i];
- }
- return sum;
- }
- void Print_Vector(VECTOR T, int sz) {
- int i;
- printf("The vector: ");
- for (i = 0; i < sz; i++) {
- printf(" %lf ", T[i]);
- }
- printf("\n");
- return;
- }
- int main(void) {
- SCALAR dProduct = 0.0;
- VECTOR x = {
- 2,
- 5.4,
- 6,
- 9
- };
- VECTOR y = {
- 4.2,
- 5.4,
- 6.7,
- 7
- };
- Print_Vector(x, N);
- Print_Vector(y, N);
- dProduct = Dot_Product(x, y);
- printf("The Dot Product of X and Y is: %10.2lf\n", dProduct);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement