I faced a question where "You are supposed to find out the product of odd integers without using any loop or it generates runtime error.
Tried using loops and expecting without loops
Related
So, By using c++ how can i write a code that keep reading numbers till a specific number and then find the avg of the previous numbers ?
I am actually making an algorithm that takes as an input a file containing tetriminoses (figures from tetris) and arranges them in smallest possible square.
Still I encounter a weird problem :
The algorithm works for less than 10 tetriminoses (every time) but start crashing with 11, 12... (I concluded that it depends on how complicated the solution is, as it finds some 14 and 15 solutions).
But the thing is, if I add an optimisation flag like -Ofast (the program is written in C) it works for every input I give him no matter how much time it takes (sometimes more than a hour..).
First I had a lot of leaks (I was using double linked list) so I changed for an Int Array, no more leaks, but same problem.
I tried using the debugger but it makes no sense (see picture) :
Debugger says my variables do not exist anymore but all I do is increment or decrement them.
For this example it just stopped while everything is fine (values of variables are correct)
Here is the link of my main function (the one that do the backtracking):
https://github.com/Caribou123/fillitAG/blob/master/19canplace/solve.c
The rest of program (same repository) consist of functions to put tetriminoses in my array, remove them from it, or print the result.
Basically I try placing a tetri, if I have enough space, I place the next one, otherwise I remove the last one and place it to the next available position etc..
Also I first thought that I were trying to place something outside of the Array, so now my Array is way bigger than it should be and filled with -1 for invalid cases (so in the worst case I just rewrite a -1), 0 for free ones, and integers values from 1 to 26 for figures.
The fact that the program works with the flag -Ofast really troubles me, as the algorithm seems to work perfectly, what could cause my program to crash ?
Here is how I tracked the number of recursions, by adding two static variables
And here is the output
(In case you want to test it yourself, use the 19canplace folder, and compile with : gcc *.c libft/libft.a)
Thanks in advance for your time,
Artiom
I'm taking a basic programming class and we can do practice programs as we go to better ourselves. Right now, I'm trying to write a program that uses a while loop to ask for inputs from a user then the program decides if those inputs meets a certain value or not. The trouble I'm having with is the output.
For example, "You have met this certain number X times out of Y times."
What do you use to keep track of how many inputs was put in and how many met the criteria?
Typically you'd use a pair of int variables for that. Initialize both to zero, then increment one each time through the loop that reads the input (so it counts how many inputs there were), and increment the other within an if statement that checks whether the input meets the criteria (so it counts how many inputs met the criteria). At the end of the loop, the two variables hold the numbers that you want to print.
Need some good problems which students can think of and apply their own logic to solve them using control instructions only. The topics covered until now are basic, not even arrays are done yet. But, I want students to be perfect before proceeding to higher topics.
I tried searching for some example problems, none were as I expected / they were the ones which I already knew.
Some of which I know:
Write a program to find out the value of a^b without using built in functions.
Write a program to find out Armstrong numbers between a range.
Write a program to print binary equivalent of a number in reverse order (since arrays are not yet done, just simple logic to print the remainder and divide the number further)
Count all -ve, +ve and 0 numbers entered by user until user wishes to terminate the program.
Write a program to display all divisors of a given number.
Write a program to find if the given number is prime or not.
Check if the given number is odd or even.
Need more good logically interesting problems which would help students to build their problem solving capability.
Thanks.
PS: Please forgive me if this question is vague or not to the point coz this question has scope for vast answers and I cannot accept a single answer, I guess?
Check if number is a palindrome (1234554321)
Rewrite a function using write() to print a number in the console (similar to printf("%d", ...))
A function that writes all combinations of 2 digits starting from 12 to 89, not allowing twice the same digit, nor a different order (12, 13, ..., 19, 23, 24... : skipping 21 because it's done with 12)
A function that write all combinations of n digits (n given as a parameter from 1 to 9) with the same rules (without using arrays)
Print first 33 terms of Fibonacci-Series
Write factorial of n being input from keyboard on console.
Find hours,minutes,seconds from given seconds.(305 s = 5m + 5s ....)
Calculate dot-product and cross-product of two 2D vectors.
Find the intersecting point of two lines(m=slope, (x0,y0)=a point for each line)
Calculate sin(pi/4) with using series expansion
Print the minimum of values given from keyboard on screen.
Simulate **and** , **or** and **xor** gates.
Find projection of a vector(3D) on another vector.
Find area of a polygon(2D)
Calculate the integral of x-square between x=0 and x=3 using integration by trapezoidal rule
Find roots of: (x-square) plus (two times x) plus (one) equals (zero)
first of all i want to prove that this is not a homework http://computer.atlas4e.com/
i really need to understand this with an explanation, because I'm trying to understand the basic of the computer by my self thanks for the help
`Prime Number List Program
Write a main program that generates a list of prime numbers. The program should use the PrimeTest subroutine to test whether each of the numbers 2, 3, 4, 5, 6…, 255 & 256 is a prime. Each number that is found to be a prime should be added to a list in memory. The list of primes should be stored in consecutive memory locations. Use a location named StartPrimeList to point to the start address of the prime number list. Use a pointer called PrimeListPtr to point to the (current) end of the list, PrimeListPtr will be incremented every time a prime number is appended to the list.
Use a location named PrN to store the number that you are checking. Start by storing a 2 in PrN. In a loop, you should call PrimeTest to determine whether PrN is a prime. If PrN is a prime, then append it to the list. Then add 1 to PrN and jump back to the start of the loop to test the next value of PrN.`
The output of the program should appear starting at location StartPrimeList as follows:-
Address Contents
[StartPrimeList] 2
[StartPrimeList+1] 3
[StartPrimeList+2] 5
[StartPrimeList+3] 7
…
…
[StartPrimeList+?] Largest Prime <= 256
(251 - 54th prime number starting from 2 <=256)
(257 - 55th prime number >256)
`
Mark Allocations for Program 2
• A program header in the program listing explaining how the program works.
• Sensible/Relevant program comments on sections of assembler language.
• Relevant labelling of loops and data items. [4]
• Program runs successfully on xComputer and produces correct output for given input.
`
"Write a main program that generates a list of prime numbers." This is your basic question. further details are given later to help you answer it.
"The program should use the PrimeTest subroutine to test whether each of the numbers 2, 3, 4, 5, 6…, 255 & 256 is a prime." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"Each number that is found to be a prime should be added to a list in memory." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"The list of primes should be stored in consecutive memory locations." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"Use a location named StartPrimeList to point to the start address of the prime number list." You should have got the message by now. Use the excellent advice given in the question itself to help you. Write your code. If it works, then fine. If it doesn't then post it here, explaining what it should do and what you think the problem might be. Tell us what you have tried, and what didn't fix the problem. That will help us having to repeat your work.
We won't write your program for you. You have to write it; we will help you write it.