-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment3.c
107 lines (86 loc) · 2.66 KB
/
Assignment3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Michael Jeffrey Flynt
CSE 240 Programming Assignment #3
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////////////////
// //
// This is the group of functions and methods needed to complete this assignment. //
// //
///////////////////////////////////////////////////////////////////////////////////////////
// It increases the number at the parameter index within the array by the parameter amount.
void increaseNumber(double * array, int arraySize, int index, double amount)
{
if(index < arraySize && index >= 0)
{
*(array + index) += amount;
}
else
{
printf("Array index out of bounds.\n");
exit(1);
}
}
// It decreases the number at the parameter index within the array by the parameter amount.
void decreaseNumber(double * array, int arraySize, int index, double amount)
{
if(index < arraySize && index >= 0)
{
*(array + index) -= amount;
}
else
{
printf("Array index out of bounds.\n");
exit(1);
}
}
// It prints all elements in the array horizontally.
void printArray(double * array, int arraySize)
{
int i;
for(i = 0; i < arraySize ; i++)
{
printf("%.2f \t",*(array + i));
}
printf("\n");
}
//////////////////////////////////////////
// main program - mostly provided code. //
//////////////////////////////////////////
int main()
{
int i;
double * numArray;
int size;
double num;
int increaseIndex;
double increaseAmount;
int decreaseIndex;
double decreaseAmount;
printf("Please enter a number of floating numbers to be entered:\n");
scanf("%d", &size);
/*** Enter a line of code to allocate memory for the array here ***/
numArray = (double *)malloc(sizeof(double));
for (i=0; i<size; i++)
{
scanf("%lf", &num); //read in a double entered by user
*(numArray+i) = num;
}
printArray(numArray, size);
printf("Please enter an index to increase:\n");
scanf("%d", &increaseIndex);
printf("Please enter an amount to increase:\n");
scanf("%lf", &increaseAmount);
increaseNumber(numArray, size, increaseIndex, increaseAmount);
printArray(numArray, size);
printf("Please enter an index to decrease:\n");
scanf("%d", &decreaseIndex);
printf("Please enter an amount to decrease:\n");
scanf("%lf", &decreaseAmount);
decreaseNumber(numArray, size, decreaseIndex, decreaseAmount);
printArray(numArray, size);
return 0;
}