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 8108915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:15:29+00:00 2026-06-06T01:15:29+00:00

i have this code and its working but i want it to return dailyTotals

  • 0

i have this code and its working but i want it to return dailyTotals list with four values, at the moment it is being returned with three values … i cant seem to figure out how to make it return dailyTotals with four values. BTW the billTotals and dateList are of the same lengh which is currently 7 and they will grow. billTotals and dateList will always remain the same length

List<double> dailyTotals = new List<double>();

        string dateValue = "";
        double oneDayTotal = 0;
        int n = 0;

        for (int i = 0; i < billTotals.Count; i += n)
        {
            dateValue = dateList[i];
            oneDayTotal = 0;

            while (dateValue == dateList[n])
            {
                oneDayTotal += billTotals[n];

                n++;
            }

            dailyTotals.Add(oneDayTotal);
        }

        return dailyTotals;

[EDIT]: Im sorry i should have written this before :/
in the database i have billTotals and the date for each bill stored. so, one date can have multiple bills associated with it. what im trying to do is grab one months data and sum the totals for each day. so the logic i used in the while loop is supposed to sum up the totals while the date is the same … i hope this makes the scenario more clear. 🙂

  • 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-06-06T01:15:30+00:00Added an answer on June 6, 2026 at 1:15 am

    You never reset n, so you are increasing i by increasingly large amount, skipping over some numbers.

    You need to set n = 0 inside the loop.

    int n = 0;
    for (int i = 0; i < billTotals.Count; i += n)
    {
        dateValue = dateList[i];
        oneDayTotal = 0;
    
        n = 0;  // This counts the number of equal dates.
        while (i + n < dateList.Length && dateValue == dateList[i + n])
        {
            oneDayTotal += billTotals[i + n];
            n++;
        }
    
        dailyTotals.Add(oneDayTotal);
    }
    
    return dailyTotals;
    

    You could also rewrite the code entirely to simplify it and avoid this curious way of incrementing your loop variable. I would suggest creating an array of objects that hold both the date and the total, then you can use LINQ to solve your problem without any complex loops:

    var dailyTotals = datesAndTotals
        .GroupBy(x => x.Date)
        .Select(g => g.Sum(x => x.BillTotal))
        .ToList();
    

    You can also use Zip instead of creating a separate class, though it will be more readable with the class.

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

Sidebar

Related Questions

i have tried this code to redirect a php page.but it s not working
I have this code for splitting input list into its halves. It seems to
I have this code in my index.php. In Mozilla its ok. But in Ie
I have this code: STRB R2, [R0,R1] and its opcode is 42 54 I'm
its a little bit hard to understand. in the header.php i have this code:
sorry about input mistakes, english its not my mother lang. I have this code
I have an assignment and i need working code. Before i start i want
I have this code in C (it's for study only): char x; uint64_t total
I have this code which I have found from google. It's a adapter for
I have pieces of code like this in a project and I realize it's

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.