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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:48:42+00:00 2026-05-25T13:48:42+00:00

I have a homework problem for my C++ class and the problem wants us

  • 0

I have a homework problem for my C++ class and the problem wants us to have the user input a wavelength and then output the correct type of radiation. The point to notice is that there are more Wave Name values than there are Wave Lengths.

My solution is listed below:

const double WAVE_LENGTH[] = { 1e-11, 1e-8, 4e-7, 7e-7, 1e-3, 1e-2 };
const char* WAVE_NAME[] = { "Gamma Rays", "X Rays", "Ultraviolet", "Visible Light", "Infrared", "Microwaves", "Radio Waves" };

double waveLength;

std::cout << "Enter a wavelength in decimal or scientific notation\nWavelength: ";
std::cin >> waveLength;

for (unsigned short i = 0U; i < 6U; ++i)
{
    if (waveLength < WAVE_LENGTH[i])
    {
        std::cout << "The type of radiation is " << WAVE_NAME[i] << std::endl;
        break;
    }
    if (i == 5U) // Last iteration
        std::cout << "The type of radiation is " << WAVE_NAME[i + 1] << std::endl;
}

My question is regarding my approach at solving the problem, specifically within the loop. I can’t seem to find a way to handle all the situations without creating two conditions inside the loop which seems like it is a poor design. I realize I could use a series of if/else if statements, but I figured a loop is cleaner. Is my approach the best way or is there a cleaner way of coding this?

Thanks!

  • 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-25T13:48:43+00:00Added an answer on May 25, 2026 at 1:48 pm

    I think you can simplify your loop to this:

    unsigned short i;
    
    for (i = 0U; i < 6U; ++i)
    {
        if (waveLength < WAVE_LENGTH[i])
        {
            break;
        }
    }
    std::cout << "The type of radiation is " << WAVE_NAME[i] << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework problem that asks: Desgin a derived class GraduateStudent from base
I have a homework problem for my algorithms class asking me to calculate the
I'm trying to work on a homework problem where I have an input date
Okay, so I have another question on a prolog homework problem I am struggling
I have a homework assignment where I need to take input from a file
In my homework, I have to design a class Message; among other attributes, it
I have a homework problem where I have to make an inheritance program that
I have done a homework assignment, here is the problem statement: Your program should
I'm working on some homework for my compiler class and I have the following
In my homework for my C class, we have to write a program that

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.