What's new

Need help with this small program.

  • Thread starter adbdragonmaster
  • Start date
  • Views 1,077
adbdragonmaster

adbdragonmaster

Enthusiast
Messages
579
Reaction score
90
Points
105
Sin$
7
FOR C/C++

I am having trouble doing this program.


Write a small program that fills an array of doubles with user input, prints the doubles on the screen in a column, and adds up all the doubles in the array and prints the sum onto the screen. You must use at least 3 functions

- Declare an array of doubles of size 20.

- Ask the user how many doubles to enter <= 20.

- Use a loop to read the doubles into the array from the user.

- Use a loop to print the array onto the screen in a column.

- Use a loop to add up all the items in the array and store the sum

- print the sum onto the screen


My specific questions:

How do you make a loop that changes the array to the next one so that the user can keep entering?

What functions do you suggest?

Really all I need is the first question, and I should be able to do the rest. Thank you.
 
Z61

Z61

Some times our saints are sinners
Retired
Programmer Forum Addict Odysseus' Summit
Messages
5,468
Reaction score
3,429
Points
1,042
Sin$
0
Aren't you in a class for this? Shouldn't this stuff be covered?
 
adbdragonmaster

adbdragonmaster

Enthusiast
Messages
579
Reaction score
90
Points
105
Sin$
7
Aren't you in a class for this? Shouldn't this stuff be covered?
Yes, but can't I ask a question? I'm not currently in the class so I'm asking different people.
 
Im4eversmart

Im4eversmart

The hacks are real
Glitcher Modder Programmer
Messages
2,156
Reaction score
1,903
Points
455
Sin$
7
You can access different objects in the array by using array_name[index].
Double array[20] will give you objects with an index between 0-19 inclusive.
You can use a for loop to iterate the index, or a while loop with your own counter.
 
adbdragonmaster

adbdragonmaster

Enthusiast
Messages
579
Reaction score
90
Points
105
Sin$
7
You can access different objects in the array by using array_name[index].
Double array[20] will give you objects with an index between 0-19 inclusive.
You can use a for loop to iterate the index, or a while loop with your own counter.


for (i = 0; i < num; i++)
{
scanf("%lf", array)
}


for (i = 0; i < num; i++)
{
scanf("%lf", array)
}
NEVERMIND forgot the "address of" &

Ok guys I still need help. For some reason my program keeps crashing. This is what I just made up

//Array.c

//Write a small program that fills an array of doubles with user
//input, prints the doubles on the screen in a column, and adds up
//all the doubles in the array and prints the sum onto the screen.
//You must use at least 3 functions

//- Declare an array of doubles of size 20.
//- Ask the user how many doubles to enter <= 20.
//- Use a loop to read the doubles into the array from the user.
//- Use a loop to print the array onto the screen in a column.
//- Use a loop to add up all the items in the array and store the sum
//- print the sum onto the screen

void arrayRead(double, int*);

void arrayList(double, int*);

void arraySum(double, int*, int*);

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>

int main()
{
int num, sum=0;
double array[20];
printf("How many doubles (numbers) would you like in the array (20 max)?\n");
scanf("%d", &num);
printf("Thank you! Now give me %d different doubles (numbers) please!\n", num);

arrayRead(array[20], &num);

printf("Here are all of your integers again!\n");

arrayList(array[20], &num);

arraySum(array[20], &num, &sum);
printf("The sum of these numbers = %d\n", sum);
return 0;
}

void arrayRead(double array[], int* num)
{
for (int i = 0; i < *num; i++)
{
scanf("%lf", &array);
}
}

void arrayList(double array[], int*num)
{
for (int i = 0; i < *num; i++)
{
printf("%.2f\n", array);
}
}

void arraySum(double array[], int*num, int* sum)
{
for (int i = 0; i < *num; i++)
{
*sum = array + *sum;
}
}

You can access different objects in the array by using array_name[index].
Double array[20] will give you objects with an index between 0-19 inclusive.
You can use a for loop to iterate the index, or a while loop with your own counter.
Can you help me out? I'm almost done
 
Last edited by a moderator:
J

jason2005

Newbie
Messages
1
Reaction score
0
Points
35
Sin$
0
for (i = 0; i < num; i++)
{
scanf("%lf", array)
}



NEVERMIND forgot the "address of" &

Ok guys I still need help. For some reason my program keeps crashing. This is what I just made up

//Array.c

//Write a small program that fills an array of doubles with user
//input, prints the doubles on the screen in a column, and adds up
//all the doubles in the array and prints the sum onto the screen.
//You must use at least 3 functions

//- Declare an array of doubles of size 20.
//- Ask the user how many doubles to enter <= 20.
//- Use a loop to read the doubles into the array from the user.
//- Use a loop to print the array onto the screen in a column.
//- Use a loop to add up all the items in the array and store the sum
//- print the sum onto the screen

void arrayRead(double, int*);

void arrayList(double, int*);

void arraySum(double, int*, int*);

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>

int main()
{
int num, sum=0;
double array[20];
printf("How many doubles (numbers) would you like in the array (20 max)?\n");
scanf("%d", &num);
printf("Thank you! Now give me %d different doubles (numbers) please!\n", num);

arrayRead(array[20], &num);

printf("Here are all of your integers again!\n");

arrayList(array[20], &num);

arraySum(array[20], &num, &sum);
printf("The sum of these numbers = %d\n", sum);
return 0;
}

void arrayRead(double array[], int* num)
{
for (int i = 0; i < *num; i++)
{
scanf("%lf", &array);
}
}

void arrayList(double array[], int*num)
{
for (int i = 0; i < *num; i++)
{
printf("%.2f\n", array);
}
}

void arraySum(double array[], int*num, int* sum)
{
for (int i = 0; i < *num; i++)
{
*sum = array + *sum;
}
}


Can you help me out? I'm almost done
Did you get it to work?
 
Top Bottom
Login
Register