Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:04:12+00:00 2026-05-23T03:04:12+00:00

From C Primer Plus 5th Ed: Write a program that creates two eight-element arrays

  • 0

From C Primer Plus 5th Ed:
“Write a program that creates two eight-element arrays of doubles and uses a loop to let the user enter values for the eight elements of the first array. Have the program set the elements of the second array to the cumulative totals of the elements of the first array. For example, the fourth element of the second array should equal the sum of the first four elements of the first array, and the fifth element of the second array should equal the sum of the first five elements of the first array. (It’s possible to do this with nested loops, but by using the fact that the fifth element of the second array equals the fourth element of the second array plus the fifth element of the first array, you can avoid nesting and just use a single loop for this task.) Finally, use loops to display the contents of the two arrays, with the first array displayed on one line and with each element of the second array displayed below the corresponding element of the first array.”

I’ve written:

#include <stdio.h>

int main(void)
{
double first[8];
double second[8];
int i;
printf("Enter 8 'doubles':\n");
i = 0;
scanf("%lf", &first[i]);
second[i] = first[i];
    i = 1;
while(i < 8)
{
    scanf("%lf", &first[i]);
    second[i] = first[i] + first[i-1];
    i++;
}
for(i = 0; i < 8; i++)
{
    printf("%.2lf ", first[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
    printf("%.2lf ", second[i]);
}
}

My output is:

Enter 8 'doubles':
1
1
1
1
1
1
1
1
1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
1.00 2.00 2.00 2.00 2.00 2.00 2.00 2.00 Press any key to continue . . .

Expected output is with 1 2 3 6 12 24 48 96 on the bottom row. Can’t figure out for the life of me why it’s only working for the first iteration. What have I missed?

Thanks in advance.

EDIT: No idea as to why it’s down as ‘homework’, I tagged it as an exercise… Ah well.

EDIT2:

This code:

#include <stdio.h>

int main(void)
{
double first[8];
double second[8];
int i;
printf("Enter 8 'doubles':\n");
i = 0;
scanf("%lf", &first[i]);
second[i] = first[i];
i = 1;
while(i < 8)
{
    scanf("%lf", &first[i]);
    second[i - 1] = first[i] + first[i-1];
    i++;
}
for(i = 0; i < 8; i++)
{
    printf("%.2lf ", first[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
    printf("%.2lf ", second[i]);
}
}

This output:

1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 
2.00 2.00 2.00 2.00 2.00 2.00 2.00 -925596313493178310000... etc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:04:12+00:00Added an answer on May 23, 2026 at 3:04 am

    You need to sum each value of the first array as you loop, so why not declare a variable to hold this value, and then assign it to the corresponding element of the second variable. This makes everything much simpler.

    #include <stdio.h>
    
    int main(void)
    {
        double first[8], second[8], sum = 0.0;
        int i = 0;
    
        printf("Enter 8 'doubles':");
        while(i < 8)
        {
            scanf("%lf", &first[i]);
            sum += first[i];
            second[i] = sum;
            i++;
        }
        printf("\nfirst -  ");
        for(i = 0; i < 8; i++)
        {
            printf("%7.2f ", first[i]);
        }
        printf("\nsecond - ");
        for(i = 0; i < 8; i++)
        {
            printf("%7.2f ", second[i]);
        }
    
        return 0;
    }
    

    The other small mistake is using %lf for the printf() format string – printf() actually only uses %f for both doubles & floats

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to self-study C using C Primer Plus from Stephen Prata and one
I'm doing a programming question from C++ Primer Plus which asks me to make
I'm learning C++ at the moment, C++ Primer plus. But I just felt like
This is a great primer but doesn't answer what I need: Combining two sorted
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
From a web developer point of view, what changes are expected in the development
From a desktop application developer point of view, is there any difference between developing
From what I've read, VS 2008 SP1 and Team Foundation Server SP1 packages are
From time to time I see an enum like the following: [Flags] public enum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.