C/C++ :: Program Which Ask For X Numbers And Store Them In Array
Nov 22, 2014
This is in c. Write a program which asks for X numbers, and stores them in an array.
The program then asks the user to enter a number to look for, and tells the user how many times that number appears in the array of numbers (if any), and the array index which contain the number
Make an array of the indexes where the number was found, and then format your output to match my output.
Sample Run:
How many numbers would you like to enter: 5
Please enter a number:1
Please enter a number:2
Please enter a number:3
Please enter a number:4
Please enter a number:2
The numbers you enter were: 1, 2, 3, 4, 2
Please enter a number to find: 2
The number appears 2 times, at array ellements with indexes 1, 4
Write a program which stores 10 decimal numbers in an array. For these numbers accept input from the users. Once the array is populated do the following:
Display each elements of the array Display the sum of all the elements of array
Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.
I'm stuck on how to put the prime numbers into an array.
The input file has the numbers 1 & 100.
Here's what I have so far.
#include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; fin.open("numbers.txt");
So we're having an assignment for school where you code a Roulette game which simulates you playing and you can choose how many times you "play" to check how many times you'd win (if that makes any sense, im german :P)
Anyways, the problem is how do i store all the random generated numbers in an array while NOT doing this (it's just a snip):
Code:
void randomzahl(void){ int i; int zahl5[5]; int zahl10[10]; int zahl100[100]; int zahl1000[1000]; int zahl10000[10000]; if(runden == 5)
I have to complete a project that i want to read float numbers from keyboard and store them into an array of 10 elements.! Every time that a number stored into array i want to compare with previous one if they have +-10 difference .. I want to keep only 10 elements into my array so every time that i give value a[0] replace a[1], a[1] replace a[2],a[2] replace a[3]. . . .and a[10] deleted.. So when all elements of the array are similar with +-10 values print out the array.!
5. Four experiments are performed and each experiment produces four test results. The results are tabulated as shown below. Write a program to store these results in a two-dimensional array, A[4][5], then use a nested for-loop to compute the average of the test results for each experiment and store it in the 5th column of the array. Print the array.
6. Using the srand( ) and rand ( ) C++ library functions, fill an array of 1000 numbers with random numbers that have been scaled to the range of 1 to 100 inclusive. Then determine and display the number of random numbers having bvalues between 1 and 50 and the number having values greater than 50. What do you expect the output counts to be?
Write a C program that sorts an unsorted array of 5 integer numbers in ascending order by swapping numbers repeatedly. Specifically, first prompt the user to input 5 numbers and store the numbers in an array. Then, if the numbers in the array are not in ascending order, ask the user to provide the indices of two numbers in the array that the user wants to swap. If the user does not enter valid indices (i.e., outside 0 to4), then re-prompt the user to enter new indices. Continue prompting the user to provide indices of numbers to swap until the array becomes sorted in ascending order. Finally, when the array becomes sorted, the program should print "The array is now sorted in ascending order".
Implementation Requirements
Use the #define directive to define the size of the array.
Implement a function called "checkArrayOrder" to check whether an array is in ascending order or not. The prototype of the function is
"intcheckArrayOrder(int arr[], int size);". The function should return
1 if the array passed to the function is in ascending order0 if the array passed to the function is not in ascending order
Define the function prototype in your program.
Implement a function called "swap", whose prototype is "int swap(int arr[], int size, int i, int j);" which swaps the numbers in positions "i" and "j" of the array passed to the function and returns
1 if the indices"i" and "j" are within bounds (i.e., between 0 and 4) and the swap operation is performed correctly. 0 if the indices"i" and "j" are outside bounds so that the swap operation cannot be performed.
Define the function prototype in your program.
Use the function "checkArrayOrder" whenever your program needs to check whether an array is in ascending order or not; and the function "swap" to swap the numbers the user specifies (Note that, you should check whether the user provided indices are valid inside the function "swap" and not inside "main").
here is my code so far
Code: #define ARRAY_SIZE 5 void main() { int i, array[ARRAY_SIZE];
i have a programming problem and i am unsure of what the final part is.the question is:
Write a program that fills an array with 10 random numbers between 1 and 20, displays the 10 numbers, and finds the sum of the 10 numbers. Call the getData, displayData, and getSum functions from the main function. Output the sum from the main program by calling the getSum function within a printf statement.
i am just really unsure of what this is--getData, displayData, and getSum functions from the main function. Output the sum from the main program by calling the getSum function within a printf statement.what i have got so far is;
Code:
#include <stdio.h> #include <stdlib.h> #define ARY_SIZE 10 int main() { int randomNumbers[ARY_SIZE], sum = 0, i;
I have to make a program that takes several numbers into an array into a function, and send them back into the main function multiplied by 1.13. This is what I have so far:
#include <iostream> using namespace std; void connell(int&n[20]) main() { int num[20];
[Code] ...
I do not know how to get numbers from an array from a main program to a function.
I am trying to write a program that adds numbers to an array. The array capacity starts out at 2, then doubles whenever the size reaches capacity. The program works perfectly fine when the array needs to be expanded. So I have narrowed down the issues into this one function (_dynArrSetCapacity)
I am currently trying to write a code that will store PID numbers of a child after a fork, I have to be able to enter an amount that will be created, so far I have managed to be able to get them to print (which puts me on the right path as far as I am concerned) but I am having issues. Using the following code:
1: Sort Alphabetically 2: Sort Grades Increasing Order (Student) 3: Sort Grades Increasing Order (Project) 4: End Program Enter choice:
Why my sort is not working. Also, I want to keep the same random numbers for the continuation of the program, I don't want new randomized values when I display the table.
I have a program that stores current a low numbers. The Object is to Store the low number every time time one is presented. so say I start the function the first number is the low number. but the trouble im having it once the function is called again it starts over(low number). Is there a way I can keep the function value once the function is called again? Any better way of doing this while keeping function in a class?
double a; class rff{ public: void FGH() { doubleb=0; cout<< "pick a number"<<endl;
Rewriting a program to convert an array based list to a linked list. The program has a user guess a number [1-100] until the correct answer is guessed. I only want to give hints if a duplicate isn't guessed.
while (aGuess-> != p->guess) doesn't work and I haven't been able to troubleshoot myself.
while (aGuess->guess != p->guess) { if ( aGuess->guess > a ) { cout << "That's too high -- try again: "; } // if guess > a
Write a program which stores 10 decimal numbers in an array. For these numbers accept input from the users. Once the array is populated do the followings:
Display each elements of the array Display the sum of all the elements of array
I am working on a program which creates a large pointer array of numbers and then performs several iterations of operations in them.
Code: int * u = new int[N]; double * nu = new double[N]; int * nud = new int[N]; for (int i=0;i<M;i++){ for (int i=0;i<N;i++){ u[i]=0; nu[i]=0;
[Code]...
If M is small enough then there are no problems in the program. However once M is large enough I get the "unhanded exception":
std::bad_alloc at memory location 0x0026f728..
Since I am just reusing the same arrays, and since I am able to make it through a few iterations, I didn't think it could be a memory issue. If it is, is there a way I can clear the data completely after each iteration?
I was working on float and double data types and to see the results i wrote this program:
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { ofstream outputF("output.txt"); double a = 1; double outcome;
[Code] ....
Well I understand the part it cannot store infinite numbers. but if you take a look at the output for example (since it is too long i just added some of the outputs)
//--------------------- for the value of : 001 1 //--------------------- for the value of : 002 0.5 //--------------------- for the value of : 003 0.3333333333333333148
[Code] ....
if you look carefully at the value "5" and "10" results. it is awkwardly abnormal. which is something i couldnt understand. also it is the same with value "20", "25", "40", "50" and so on.
The program should store a character array in reverse order then display the reversed array. I have also included in the code that will display the actual characters into the array as it loops through. So I know the characters are being stored, but why doesn't it display the entire string when I call it?
I've started programming my little program called vLibrary (program I want to make for the library in my city) and after I m done with console application I will try to implement wxWidgets.My program will be able to add new users to the system, new books and new librarians.
Now, I m confused what data types to use and how to store objects (newly created users, books etc) to my program so later on they can log in the system etc. Logic of the program is completely clear to me but I m not sure how to make array of objects and store them in memory or in a certain file, how to store password and make some kind of encryption etc.which data structure from STL should I use and how.
Let's say we have entities like lines and circles. And each entity can have a pen attached. And a pen is a color and line width. All entities needs to be manipulated through a operations interface. All entities added to the program needs to be added through using a factory pattern.
So we end up something like (pseudo code)
class Line // for immutable objects { .. .. } class MutableLine extends Line //for mutable lines { } MutableLine line=factory->newLine(20,20,50,50); // Create a new line entity
[code]....
So, essentially I want no code to be able to operate directory on entities, unless during creation of the object (color, layer, line width etc...) so I am planning to create Mutable versions of all entities besides the immutable versions.
When requested for selected entities or all entities, I am planning to return a list of immutable objects and in fact I am planning to return a new copy so it's not possible to operate on anything directly.
The reason is that I am planning to create different storage backends swell, so I can operate on entities in a database, or shared memory... stuff like that. At least to hide the internals and provide a stable API.
My questions are:
How can I make sure that people don't do 'tricks' casts for example to a mutable version to change objects directly?