That is my code. But the output is wrong. My expected output is:
input: 1 2 3
output: 3 2 3;
but the actual output is 2 1 0 on my code.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n; // create a variables to decide how many number that they want to store on array
int position[10];
int sumdiffs[10];
printf("How many number you want to enter here: ?\n"); // let users enter how much number
scanf("%d", &n);
// accept users number
for (int m = 0; m < n; m++) {
printf("Please enter number %d:", m + 1);
scanf("%d", &position[m]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sumdiffs[i] = abs(position[i] - position[j]);
}
printf("%d\n", sumdiffs[i]);
}
return 0;
}
That is my test.
How many number you want to enter here: ?
3
Please enter number 1:1
Please enter number 2:2
Please enter number 3:3
2
1
0
You are not computing the sum of differences, you only store each difference so the array sumdiffs contains the last difference.
Note also that your code has undefined behavior for n greater than 10 because you access the arrays beyond their boundaries.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n;
printf("How many numbers you want to enter here?\n");
if (scanf("%d", &n) != 1 || n <= 0)
return 1;
// arrays defined with the correct size (hopefully not too large)
int position[n];
int sumdiffs[n];
// read the numbers from the user
for (int m = 0; m < n; m++) {
printf("Please enter number %d: ", m + 1);
if (scanf("%d", &position[m]) != 1)
return 1;
}
for (int i = 0; i < n; i++) {
sumdiffs[i] = 0;
for (int j = 0; j < n; j++) {
sumdiffs[i] += abs(position[i] - position[j]);
}
printf("%d\n", sumdiffs[i]);
}
return 0;
}
First, clear the accumulated sums:
for (int i = 0; i < n; i++) {
sumdiffs[i] = 0;
Then, just change
sumdiffs[i] = abs(position[i] - position[j]);
into
sumdiffs[i] += abs(position[i] - position[j]);
to accumulate the differences.
Related
I want the user to input values of elements to an array of 10 ints. Then I want to print out all the indexes of the values of the elements which when divided by 5 give 0.
I've tried doing it like this, but it's not working. How do I append the i's into an array?
#include <stdio.h>
#define N 10
int main()
{
int a[10];
int i;
int index[10] = {};
printf("Input %d values into the array:\n", N);
for (i = 0; i < N; i++){
scanf("%d", &a[i]);
if (a[i] % 5 == 0){
index[10] += i;
}
}
printf("%d", index);
return 0;
}
You can create a new index for the second array and solve the problem. But is better to make it in different loops.
#include <stdio.h>
#define N 10
int main()
{
int a[N];
int index[N] = {};
//o will be the index for index array
int o = 0;
printf("Input %d values into the array:\n", N);
for (int i = 0; i < N; i++){
scanf("%d", &a[i]);
if (a[i] % 5 == 0){
index[o] = i;
o++;
}
}
for(int i = 0; i<o;i++){
printf("%i",index[i]);
}
return 0;
}
Your last comment clarified the intention of your code so here is how solve it directly without the restoring to dynamic memory allocation:
#include <stdio.h>
#define N 10
int main(void) {
int result[N];
int n = 0;
for (int i = 0; i < N; i++){
int d;
if(scanf("%d", &d) != 1) {
printf("scanf failed\n");
return 1;
}
if (!(d % 5)) {
result[n++] = i;
}
}
for(int i = 0; i < n; i++) {
printf("%d%s", result[i], i + 1 < n ? "," : "\n");
}
}
and example run:
echo "1 2 3 4 5 6 7 8 9 10" | ./a.out
4,9
I am new to programming, I am trying to write a program that lets the user input numbers ranging from 0 to 1000, and the maximum number the user can input is 100. The numbers in the array don't have to be in order, and the program ends when the user inputs a negative number. After that, the program should determine which number occurs the most and the frequency of that occurrence.
I have written a similar code but not for this type of problem the code below showcases what I mean by similar code and any help would be appreciated
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char again;
do {
srand(time(0));
int myNumbers[10];
int i, n, findnum, time, num;
n = 10;
for (i = 0; i < n; i++) {
myNumbers[i] = rand() % 10 + 1;
}
for (i = 0; i < n; i++) {
printf("elements %d\n", myNumbers[i]);
}
printf("Enter number to find Occurrence: ");
scanf("%d", &findnum);
time = 0;
for (i = 0; i < n; i++) {
if (myNumbers[i]==findnum)
time++;
}
if (findnum>0) {
printf("Occurrence of %d is: %d times\n",findnum,time);
} else {
printf("The number %d is not present in the array\n",num);
}
do {
printf("Shall we play again (y/n)?: ");
while(getchar()!='\n');
scanf("%c", &again);
}
while(again !='y' && again !='n');
}
while(again =='y');
}
You will need a second array to count the frequencies. Worst case, the user entered unique numbers, so the second array should be as large as myNumbers. The array will hold two values: the number, and its count:
int myNumbers[10];
int myCount [10][2] = {0};
int n= 10;
You remember the first entry of myCount that is available:
int m= 0;
You cycle over all numbers:
for (i = 0; i < n; i++){
For each number, you check if it is already in the myCount and if yes, increment the count and then exit the loop:
for (j = 0; j < m; j++){
if (myCount[j][0] == myNumbers[i]){
myCount[j][1]++;
break;
}
}
If the number was not found, you add it:
if (j == m) {
myCount[m][0] = myNumbers[i];
myCount[m][1] = 1;
m++;
}
}
Now you can search the array for the number with the highest count.
Integrated the code is:
int myNumbers[10];
int myCount [10][2] = {0};
int n= 10;
int m= 0;
/* now fist read the input */
for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
if (myCount[j][0] == myNumbers[i]){
myCount[j][1]++;
break;
}
}
if (j == m) {
myCount[m][0] = myNumbers[i];
myCount[m][1] = 1;
m++;
}
}
To do: search the array for the number with the highest count.
Currently stuck on creating a pyramid out of hashes (#'s) based on a number given by user input. The example for CS50 only describes how to create a square based on the number given.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Number:\n");
if(n>0 && n<9)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
Expected result is to create a pyramid that is x amount of #'s wide and tall based on the input given by user.
Actual result is a square that is x amount of #'s wide and tall based on the input given by user.
You need a loop which prints spaces until the second loop's counter (j) is less than n-i. Please see below:
#include <stdio.h>
int main(void)
{
int n, i, j, k;
printf("Number: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n-i; j++)
{
printf(" ");
}
for (k =0; k <= i; k++)
{
printf("# ");
}
printf("\n");
}
} // end main function
I am trying to run the code below whilst using the debugger. At the end of the following loop "for (i=0;i<n;i++) pin[i]=0;", n's value changes from the value I've given it and becomes 0. I can't understand why that happens, so your help as to why it happens would be greatly appreciated. Oh, and one other thing. If I ignore it and as soon as I've given n a value, I assign that value to another integer, in order to be able to use it when n becomes 0, my program crashes. It's a crash of the type you get when, for example, you're using a variable you've not assigned a value to.
main()
{
int i,j,k,n,pin[n];
printf("Give the size of the array:\n");
scanf("%d", &n);
do{
printf("Give the number of the iterations:\n");
scanf("%d", &k);
}while (k<1||k>n);
for (i=0;i<n;i++)
pin[i]=0;
for (j=0;j<k;j++){
for (i=0;i<n;i++){
if (i%j==0){
if (pin[i]==0)
pin[i]=1;
else
pin[i]=0;
}
}
}
for (i=0;i<n;i++)
printf("%d ", pin[i]);
}
You must not divide by 0 and define pin[n] where n is initialized.
#include <stdio.h>
int main() {
int i, j, k, n;
printf("Give the size of the array:\n");
scanf("%d", &n);
int pin[n];
do {
printf("Give the number of the iterations:\n");
scanf("%d", &k);
} while (k < 1 || k > n);
for (i = 0; i < n; i++)
pin[i] = 0;
for (j = 0; j < k; j++) {
for (i = 0; i < n; i++) {
if (j != 0 && i % j == 0) {
if (pin[i] == 0)
pin[i] = 1;
else
pin[i] = 0;
}
}
}
for (i = 0; i < n; i++)
printf("%d ", pin[i]);
}
Test
Give the size of the array:
3
Give the number of the iterations:
2
1 1 1
Test 2
Give the size of the array:
5
Give the number of the iterations:
4
1 1 0 0 0
I have written some code to ask the user for n, then print the prime numbers up to n. However when I use it, i.e with 10, it only prints the non-prime numbers
/* Asks for the amount of prime numbers you would like to print, then prints them */
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
check = 0;
for (j = 2; j < i ; j++) {
if (i % j == 0) {
check = 1;
if (check == 1) {
printf("%d\n", i);
}
}
}
}
return 0;
}
How many prime numbers would you like to print? 10
4
6
6
8
8
9
10
10
I've tried everything but I think I am missing something really trivial!
This is how it should be:
for (i = 2; i <= n; i++)
{
check = 0;
for (j = 2; j < i ; j++)
{
if (i % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
printf("%d\n", i);
}
}
Also, in the inner loop you don't have to divide the number till j < i. You don't have to go beyond i/2.
As Weather Vane said, the mod operator % returns 0 if i is exactly divisible by j and if this is true then the number is not prime. Your conditional statement is backwards.
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++)
{
check = 0;
for (j = 2; j < i ; j++)
{
if (i % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
printf("%d\n", i);
}
}
return 0;
}
How many prime numbers would you like to print? 10
2
3
5
7
Several problems.
First, when you set check = 1, that means that i divides evenly, so n is not prime, so you shouldn't print it. You should be printing the number when check == 0.
Second, you're printing each time through the inner loop. You should test check at the end of the loop, to ensure that none of the numbers divided it.
As an improvement, there's no need to keep checking once you find one number that divides evenly. So you can break out of the inner loop as soon as you set check = 1.
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
check = 0;
for (j = 2; j < i ; j++) {
if (i % j == 0) {
check = 1;
break;
}
}
if (check == 0) {
printf("%d\n", i);
}
}
return 0;
}
try looking at this code
#include <stdio.h>
int IsPrime(int num)
{
int i = 2;
for (i = 2; i < num; i++) if (num % i == 0) return 0;
return 1;
}
int main(void)
{
int n, i;
char *nStr = (char*)malloc(10);
printf("How many prime numbers would you like to print? ");
fgets(nStr, 9, stdin);
n = atoi(nStr);
for (i = 1; i <= n; i++) if (IsPrime(i)) printf("%d\n", i);
getchar();
return 0;
}
and about your code, you should print the number only if check remains 0.