Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming: Arrays, Random Number Generation, and Applications, Slides of Computer Fundamentals

Examples of using arrays in c programming, including character arrays and string manipulation. It also discusses the generation of pseudorandom numbers in c, including the use of the srand function and the time function to randomize the seed. Code examples for a students poll program and a histogram program.

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.
1
6.4 Examples Using Arrays
Character arrays
String “first” is really a static array of characters
Character arrays can be initialized using string literals
char string1[] = "first";
Null character '\0' terminates strings
string1 actually has 6 elements
It is equivalent to
char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };
Can access individual characters
string1[ 3 ] is character ‘s’
Array name is address of array, so & not needed for scanf
scanf( "%s", string2 );
Reads characters until whitespace encountered
Can write beyond end of array, be careful
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C Programming: Arrays, Random Number Generation, and Applications and more Slides Computer Fundamentals in PDF only on Docsity!

6.4 Examples Using Arrays

  • Character arrays
    • String “first” is really a static array of characters
    • Character arrays can be initialized using string literals

char string1[] = "first";

  • Null character '\0' terminates strings
  • string1 actually has 6 elements
    • It is equivalent to

char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

  • Can access individual characters

string1[ 3 ] is character ‘s’

  • Array name is address of array, so & not needed for scanf

scanf( "%s", string2 );

  • Reads characters until whitespace encountered
  • Can write beyond end of array, be careful

Outline

fig06_10.c (Part 1 of 2)

Example: Students poll

  • Write a program that uses poll record of 40

students. Each student have entered a value

between 1 and 10. Count frequency of each choice

in the given data set.

  • Data set size 40
  • Range 1-10 (ten choices)

Outline

fig06_07.c (Part 1 of 2)

Example: Histogram

  • A histogram is a bar graph, where we divide the

grades into ranges and plot the number of grades

in each range. Your histogram should have 5 bars,

for the ranges 100-90, 89-70, 69-50, 49-35 and

34-0.

Outline

fig06_08.c (Part 1 of 2)

rand ( )

FUNCTION

# include < stdlib.h >

Returns "random" number between 0 and

RAND_MAX (at least 32767)

CALLING:

int x = rand ( ) ;

A call goes to " rand ( ) " , it generates a number

and returns to x

rand ( )

Rolling a die

  • It returns the remainder

rand ( ) % 6 =?

Result has to be between 0 and 5 inclusive

1 + rand ( ) % 6

It will randomly generate number between

1 and 6

It has only two possibilities 0 / 1

rand ( ) % 2 ;

Example: Tossing a Coin