首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
BIT 1400程序讲解、讲解C/C++语言编程、c++程序辅导 讲解R语言编程|讲解数据库SQL
项目预算:
开发周期:
发布时间:
要求地区:
BIT 1400 Tutorial 9:
The Sizeof Arrays
This tutorial will examine arrays, memory and the sizeof operator.
Task 1: Sizeof Arrays
For task 1 you will make a couple dynamic and static arrays, use the sizeof operator and print out the
sizeof values you get.
1. Make a static array of ints: int a_staticNums[50]; What is sizeof(a_staticNums) ? Why?
2. What is sizeof(a_staticNums[0]); ? Why?
3. Make a dynamic array of 20 characters char* a_dynChars using malloc.
4. Make a dynamic array of 40 integers int* a_dynNums using calloc.
5. What is sizeof(a_dynChars) ? What is sizeof(a_dynChars[0]) ? Notice the values are not 20?
6. What is sizeof(a_ dynNums) ? What is sizeof(a_ dynNums [0]) ?
7. What is sizeof(char*) ? sizeof(int*)?
8. What does sizeof calculate? Can you use it to determine the size of an array? (short answer: no)
9. Write a function that prints (printf) the contents of any array of integers….besides the array of
integers, what other parameter does the function need? Why do we NOT need that function for
a character string? Is there an equivalent to ‘\0’ for integers? (short answer: no)
10. Set the 10th element of a_dynNums to be 41 without using the square parentheses ([]). This will
require pointer arithmetic.
11. Make sure you call free on a_dynChars and a_dynNums when you are finished with the arrays.
Hopefully it is now clear why you can’t use sizeof to determine the size of the array. C is so old that you
need to pass the array size into your functions too.
Task 2: Ninja Editing
You will now write 3 different functions, all of which use a student struct
1. Create a “student” struct which contains a char array name which has 50 elements, an unsigned
int ID and an unsigned int age (so 3 variables in the struct).
2. In main, create a student struct named testStudent and set the values to whatever you like.
Recall that for the name you should use the strcpy function to set the name.
3. Make a new function named peasantEdit which takes a student struct s and returns void. Your
job is to edit s in the function.
a. Call peasantEdit(testStudent) and confirm testStudent hasn’t been changed despite
what you did in the function. A printf statement will show testStudent didn’t change.
b. peasantEdit makes a new struct and copies the values from testStudent. Hence
testStudent doesn’t change.
4. Write a new function named ninjaEdit which takes a student struct pointer ps (student* ps). Edit
the values of the struct ps points to (ps->ID = 1234; would change the ID for example.
a. Call ninjaEdit(testStudent) and confirm testStudent DOES change.
5. Extra Challenge: This is not C code (it’s C++) but you can “pass by reference” to change a
variable without using a pointer. Write a function ninjaEdit2 that takes a student struct
reference: void ninjaEdit2(student& s). Change some values in s. For example s.ID = 1234;
a. Call ninjaEdit2(testStudent) and confirm testStudent has changed yet again.
b. Notice that pass by reference doesn’t hint to the person calling the function that their
variable could be changed by the function?? That’s the danger of pass by reference.
HINT:
const int MAX_NAME_LEN = 50;
struct student
{
char name[MAX_NAME_LEN];
//………………..
};
// Passing in a student actually makes a copy of the structure
// thus the original is unchanged
void peasantEdit(student s);
// Passing in a pointer means you can edit the original structure
void ninjaEdit(student* s);
// Pass by reference means you edit the original structure...and thus this
// approach should be done with caution.
void ninjaEdit2(student& s);
Task 3 (if you have time)
• Make an array of floats, make a dynamic array of floats, and make an array of float pointers. All
3 arrays should be the same size. Use sizeof for each. Write the sizes as a COMMENT your code.
float a_staticF[10] = { 1.0f, 2.3f, 2.0f, 4.0f, 17.0f,
-23.9f, -8.0f, 0.0f, 927.4f, 0.0f };
• Make each float in your dynamic array equal the value in the static array.
• Make each pointer in your float pointer array point to values in your dynamic float array.
• Print the values at each index for each array. For the array of float pointers, output the value
of the pointee.
• Change several random values in the static and dynamic float arrays.
• Print the values of each index for each array again. What values changed?
• Make sure you free the two dynamic arrays.
Submitting your work:
When you are finished (no matter how far you get), please submit your .cpp file cuLearn. Dynamic
memory use requires practice so progress as far as you can.
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写math 1151, autumn 2024 w...
2024-11-14
代做comp4336/9336 mobile dat...
2024-11-14
代做eesa01 lab 2: weather an...
2024-11-14
代写comp1521 - 24t3 assignme...
2024-11-14
代写nbs8020 - dissertation s...
2024-11-14
代做fin b377f technical anal...
2024-11-14
代做ceic6714 mini design pro...
2024-11-14
代做introduction to computer...
2024-11-14
代做cs 353, fall 2024 introd...
2024-11-14
代做phy254 problem set #3 fa...
2024-11-14
代写n1569 financial risk man...
2024-11-14
代写csci-ua.0202 lab 3: enco...
2024-11-14
代写econ2226: chinese econom...
2024-11-14
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!