logo
down
shadow

Program gets 101 numbers instead of 100


Program gets 101 numbers instead of 100

By : Royal Krow
Date : November 22 2020, 10:33 AM
wish help you to fix your issue If you start counting from zero, you will count an extra number.
0 ... 1 => this is 2 numbers
code :
for (i=0; i<100; i++)


Share : facebook icon twitter icon
Trying to ask a user to provide a set of numbers, then have program determine if the numbers are in ascending order or n

Trying to ask a user to provide a set of numbers, then have program determine if the numbers are in ascending order or n


By : user2445325
Date : March 29 2020, 07:55 AM
hop of those help? I am trying to create a program that asks the user for a set of numbers, first asking for the quantity of numbers, then having them input all the numbers. The program then checks the numbers, and determines whether or not the numbers given are in ascending order or not. Then, simply print out "yes ascending" or "no not ascending" and print out the array on one line..So far, my code will just always say that "yes, this is an increasing array!" Please look below for my code. in advance!.. , You're reading in "numbers", which you created as type String.
code :
cout << "Enter the numbers in the array, separated by a space, and press enter: " << endl;
numbers += ' ' + numbers;
cin >> numbers;
int arr[arraysize];
if ( isAscending(arr, arraysize)){....}
 int main(){
    int arraysize = 0;
    int number = 0;

    cout << "Enter the size of the array: " << endl;
    cin >> arraysize;
    if (arraysize < 1) // you should really have this loop until you get correct input
    { cout << "ERROR: you entered an incorrect value for the array size!" << endl; }
    int* arr = new int[arraysize];
    cout << "Enter the numbers in the array, separated by a space, and press enter: " << endl;

    int i = 0;
    while(i < arraysize){
        cin >> number;
        arr[i] = number;
        i++;
    };

    if ( isAscending(arr, arraysize)){ cout << "This IS an increasing array!" << endl; }
    else{ cout << "This is NOT an ascending array!" << endl;}
    for (int i = 0; i < arraysize; i++){
        cout << arr[i];
        if( (i+1) == arraysize){ cout << ". ";}
        else cout << ", ";
    }
 return 0;
 }
Finding Sum of Prime Numbers between interval (C++ program doesn't work for large numbers)

Finding Sum of Prime Numbers between interval (C++ program doesn't work for large numbers)


By : HarisKhalid
Date : March 29 2020, 07:55 AM
To fix this issue The problem you are dealing with is integer overflow.
(signed) long can on most machines have values between -2147483647 and 2147483647.
program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by one

program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by one


By : WangYue
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Your logic in the "processing" block seemed off. I modified it to check the current number (outer loop) to all of the known numbers (inner loop). If no match was found, it is appended to the list of known numbers and the count is incremented.
I also modified the "output" code to print the first counter numbers from the list of known numbers. Values past that index are uninitialized.
code :
import java.util.Scanner;
public class problem2try {

    public static void main(String[] args) {
        //declarations 
        Scanner keyboard = new Scanner (System.in);  
        int [] inputList = new int [10]; 
        int [] distinctArray = new int [10]; 
        int num; 
        int counter = 0; 

        //input 
        System.out.print("Please enter in 10 integers: ");

        for (int i = 0; i < inputList.length; i++)
        {
            num = keyboard.nextInt(); 
            inputList[i] = num; 
        }

        //processing
        for (int i = 0; i < inputList.length; i++)
        {
            boolean found = false;
            for (int j = 0; j < counter; j++)
            {
                if (inputList[i] == distinctArray[j])
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                distinctArray[counter++] = inputList[i];
            }
        }

        //output
        System.out.println("The number of distinct numbers is " + counter);
        System.out.print("The distict numbers are: ");
        for (int x=0; x<counter; x++)
        {
            System.out.print(distinctArray[x] + " ");
        }
    }
}
C++ Program to Display Numbers Between Two Intervals Check Whether a Number can be Express as Sum of Two Prime Numbers

C++ Program to Display Numbers Between Two Intervals Check Whether a Number can be Express as Sum of Two Prime Numbers


By : Alex
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You have to reset your flag at each loop otherwise once it's set with true value, this condition will never be reached if (flag == false).
code :
for (i = n1 ; i <= n2 ; i++)
{
    flag = false; /* Reset the flag */
    for (j = 2 ; j <= i/2 ; j++ )
    {
        if(checkPrime (j) && checkPrime (i-j))
        {
            cout << "Number " << i << " equal sum of two prime number " << j << " + " << i - j << endl;
            flag = true;
        }                       
    }
    if (flag == false)
    {
        cout << "Number " << i << " can't epress to sum of two prime number " << endl;
    }
}
I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers togeth

I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers togeth


By : Deepali Suryawanshi
Date : March 29 2020, 07:55 AM
Hope that helps I fixed yours so it adds all the numbers now still not sure what half of it does I added nums = nums + num.nextInt(); so it will total all nums
I created my solution as well if you want a working reference on the bottom below yours
code :
public static void main(String[] args){
    int change = 0;//Total Numbers It think
    int nums = 0;//Sum

    System.out.println("Enter the numbers to find their average: ");
    Scanner num = new Scanner(System.in);//Should be moved out of loop so its not initialized every time

    int Dividend=1;//I had to declare to run it
    while (change <= Dividend) {//not sure why your comparing these

        nums = nums + num.nextInt();//Total Sum
        change++;//Total nums

        if (change == Dividend) {
            System.out.println("Average: " + nums / Dividend);
        }
        System.out.println(nums);
    }
}
public static void main(String[] args){
    int sum = 0;
    int countOfNumbers = 0;//Tthe same as your change im guessing

    System.out.println("Enter the numbers to find their average type stop to quit: ");//Added stop statement
    Scanner scanner = new Scanner(System.in);//Should be moved out of loop so its not initialized everytime

    while (scanner.hasNext()){//Ensures there is something to check
        String next = scanner.next(); //Pull next Value
        if(next.equalsIgnoreCase("Stop"))//This will prevent looping forever
            break;
        sum = sum + Integer.valueOf(next); //Get total sum
        countOfNumbers++; //Increment total numbers
        System.out.println("The Sum is:"+sum
                +"\nThe count of Numbers averaged is:"+countOfNumbers
                +"\nThe Average is:"+sum/countOfNumbers);//Print info 
    }
    System.out.println("Done Averaging Goodbye");
}
Related Posts Related Posts :
  • Data type conversion in Postfix evaluation
  • No output in terminal (Head First C)
  • Data writes over after realloc
  • function: findnextchar() doesn't work
  • Getting file-size property from Nautilus
  • Forward declare entities in C standard library?
  • Static array in C
  • Function signature indicates return type void*, but returns pointer of a different type
  • How do I measure time per thread in C?
  • Online judgeProblem (Wrong Answer)
  • Use function pointer initialized in other source file
  • last line of file is duplicated using fscanf
  • How to print files with same month of modification using directory in c?
  • ADT Circular Queue enqueue and dequeue
  • What events can cause ferror to return non-zero?
  • Accessing dynamically allocated structure received through socket
  • Vim with C Conditional Parsing
  • C can pointers be aligned with any other data type?
  • sdcc inline asm() not working
  • I/O - Manipulation of C Strings
  • sscanf 1 byte hex data without overflow
  • Avoid division by zero in C when taking log with respect to a random number
  • C - memory corruption with threads
  • Why does my program sometimes returns 0xc0000005 and sometimes runs well?
  • C - Dynamic array handling advice
  • Does Standard define null pointer constant to have all bits set to zero?
  • valgrind report strange memory usage
  • abs without branching, why this code works
  • Heapsort Algorithm by Cormen Implementation in C , not working correctly
  • C database program can't find 1st input. Why?
  • Generate random numbers with rand() except number zero
  • How can I know how many free bits are there in a pointer?
  • Returning array pointer produces "warning: return from incompatible pointer type"
  • Gauss Seidel (Specific equation solver) in C
  • Combine 4 chars into one unsigned long
  • Linked List pointer printing memory leak
  • My program won't do search function well. Why?
  • Varnish C VRT variables/functions
  • How can I concatenate strings in C?
  • Char with a variable length
  • Structure pointer pointing to different structure instance
  • Generate random string for member of struct
  • User Level Interrupt Handler for Timer
  • Generate random number for member in struct
  • Error: incompatible types when assigning to type ‘char[25]’ from type ‘char *’
  • Can't figure out why I am getting strange output
  • Condition checking inFor loop
  • segmentation fault due to initialize() function when changing the size of the huge page allocation
  • suspending an application preemptively before out of memory causes a segmentation fault
  • Should I use "rand % N" or "rand() / (RAND_MAX / N + 1)"?
  • C programming: Sorting a integer file while keeping their original place in file
  • Why I can't store/retrieve 11 digit long number properly
  • Out of bound array elements being printed using gcc in Win8
  • FIN-PSH-ACK server-side closure provoking RST from the client
  • Basic loop ends unexpectedly
  • PIC in C - creating own keypad scan routine using RC0-3
  • Does #if #endif act like scope?
  • Why is the -- operator not subtracting from the value when executed?
  • How i can disable maximal munch rule in Lex?
  • Compiler throwing error for function return type returning structure pointer
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org