Listing numerical contents of an array in c
up vote
0
down vote
favorite
I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).
The function to calculate the amount of passing grades and print them is fine.
Printing the contents of the array using printf is where I am having problems.
This is how I input the array:
int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};
Currently I am using this (which works):
printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);
It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.
Is there a more elegant solution that I am unaware of?
I did search the topics and was unable to find an answer, sorry if this is a duplicate.
c arrays numeric
add a comment |
up vote
0
down vote
favorite
I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).
The function to calculate the amount of passing grades and print them is fine.
Printing the contents of the array using printf is where I am having problems.
This is how I input the array:
int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};
Currently I am using this (which works):
printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);
It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.
Is there a more elegant solution that I am unaware of?
I did search the topics and was unable to find an answer, sorry if this is a duplicate.
c arrays numeric
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).
The function to calculate the amount of passing grades and print them is fine.
Printing the contents of the array using printf is where I am having problems.
This is how I input the array:
int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};
Currently I am using this (which works):
printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);
It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.
Is there a more elegant solution that I am unaware of?
I did search the topics and was unable to find an answer, sorry if this is a duplicate.
c arrays numeric
I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).
The function to calculate the amount of passing grades and print them is fine.
Printing the contents of the array using printf is where I am having problems.
This is how I input the array:
int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};
Currently I am using this (which works):
printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);
It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.
Is there a more elegant solution that I am unaware of?
I did search the topics and was unable to find an answer, sorry if this is a duplicate.
c arrays numeric
c arrays numeric
edited Nov 10 at 21:42
asked Nov 10 at 21:29
Will McCoy
32
32
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32
add a comment |
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Use a control loop (in this case for
) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):
int grades[N]; // initialized here or filled later
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M
items, but only holds N
(where 0 <= N <= M
holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):
if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}
You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.
add a comment |
up vote
0
down vote
You need to write a function that receives grades as an argument and prints them!
void print_array(int* grades, int size) {
for(int i = 0; i < size; i++) {
printf("%d", grades[i]);
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use a control loop (in this case for
) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):
int grades[N]; // initialized here or filled later
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M
items, but only holds N
(where 0 <= N <= M
holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):
if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}
You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.
add a comment |
up vote
0
down vote
accepted
Use a control loop (in this case for
) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):
int grades[N]; // initialized here or filled later
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M
items, but only holds N
(where 0 <= N <= M
holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):
if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}
You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use a control loop (in this case for
) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):
int grades[N]; // initialized here or filled later
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M
items, but only holds N
(where 0 <= N <= M
holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):
if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}
You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.
Use a control loop (in this case for
) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):
int grades[N]; // initialized here or filled later
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M
items, but only holds N
(where 0 <= N <= M
holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):
if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}
You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.
answered Nov 10 at 21:48
WhozCraig
50.7k858104
50.7k858104
add a comment |
add a comment |
up vote
0
down vote
You need to write a function that receives grades as an argument and prints them!
void print_array(int* grades, int size) {
for(int i = 0; i < size; i++) {
printf("%d", grades[i]);
}
}
add a comment |
up vote
0
down vote
You need to write a function that receives grades as an argument and prints them!
void print_array(int* grades, int size) {
for(int i = 0; i < size; i++) {
printf("%d", grades[i]);
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to write a function that receives grades as an argument and prints them!
void print_array(int* grades, int size) {
for(int i = 0; i < size; i++) {
printf("%d", grades[i]);
}
}
You need to write a function that receives grades as an argument and prints them!
void print_array(int* grades, int size) {
for(int i = 0; i < size; i++) {
printf("%d", grades[i]);
}
}
answered Nov 10 at 21:33
Saeid Yazdani
6,22139132238
6,22139132238
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243593%2flisting-numerical-contents-of-an-array-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31
Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32