I want to find the smallest number from the numbers in an array entered by the user.
The output I am getting for the program is a vague number -858993460. This is generally because of a type mismatch but they seem alright here. Not able to think of a solution. Thanks for the help!
```
//
#include<stdio.h>
int main()
{
int a;
int i;
int num[25];
printf("Enter the 25 numbers:");
for (i = 0; i <= 24; i++)
{
scanf("%d", &num[i]);
}
for (i = 0; i <= 24; i++)
{
a = num[i];
if (a < num[i+1])
{
a = a;
}
else
a = num[i+1];
}
printf("Lowest number is %d\n", a);
return 0;
}
Hope this will work for you. Just remove the else statement and bring a=num[i] inside if statement and consider the first element is the minimum element by doing this a = num[0]
#include<stdio.h>
int main()
{
int a;
int i;
int num[25];
printf("Enter the 25 numbers:");
for (i = 0; i <= 24; i++)
{
scanf("%d", &num[i]);
}
a = num[0];
for (i = 0; i <= 24; i++)
{
if (num[i] < a)
{
a = num[i];
}
}
printf("Lowest number is %d\n", a);
return 0;
}
Your problem is the i+1 statement. When on i = 24, the next index is out of bounds. Instead of throwing an error, C lets you access that part of memory, giving you whatever garbage data was stored there. That's why you're getting such a random result.
Related
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.
When I ran this code why is it giving me incorrect output? In my system I'm getting the correct output. My output is the same as the one given in the link but still they wont accept it.
int main()
{
int t, n, b, i;
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &b);
int area[n];
int max = 0;
area[0] = 0;
int p[n], w[n], h[n];
int count = 0;
for (i = 1; i <= n; i++) {
scanf("%d %d %d", &w[i], &h[i], &p[i]);
}
for (i = 1; i <= n; i++) {
if (p[i] <= b) {
area[i] = w[i] * h[i];
if (area[i] > max) {
max = area[i];
printf("%d\n", max);
count++;
}
}
}
if (count == 0) {
printf("no tablet\n");
}
}
return 0;
}
You are indexing out of the array bounds with
for(i = 1; i<=n; i++)
You can index an array length n with index 0 to n-1. So change both the loops to
for(i = 0; i < n; i++)
and remove the useless line
area[0] = 0;
Also, you should not output the result inside the loop, since if the data entry sequence is different from the example it prints a result more than once. Put that afterwards.
if(count == 0)
{
printf("no tablet\n");
}
else
{
printf("%d\n",max);
}
It prints the wrong number/random number. What I want is to print numbers higher than 75.
int main() {
int array[5];
int num, i;
for (i = 1; i <= 5; i++) {
printf("Input Num %d : ", i);
scanf("%d", &num);
}
if (num >= 75) {
printf("%d\n", array[i]);
}
return 0;
}
Please use if within "for" loop. and please change "array" to "arr" or another name. The array will be a keyword in c++ sometime. should not use "array" to name a variable. here is my solution:
int main() {
int arr[5];
int num, i;
for (i = 1; i <= 5; i++) {
printf("Input Num %d : ", i);
num = 0;
scanf("%d", &num);
arr[i-1] = num;
}
for (i = 1; i <= 5; i++) {
if (arr[i - 1] >= 75) {
printf("%d\n", arr[i - 1]);
}
}
return 0;
}
You have a couple of errors:
You iterate over the wrong set. Arrays in C start from 0, NOT 1.
You never set any value for array. What should it contain then? Random stuff of course. You could avoid it by initializing your array to zero. That way you know you are not writing to your array if you get zeros in your output.
Code:
#include <stdio.h>
int main() {
int array[5] = {};
int num = 0,i;
for ( i = 0; i <5; i++) {
printf("Input Num %d : ",i );
scanf("%d",&num );
array[i] = num;
}
for ( i = 0; i <5; i++) {
if (array[i] >= 75) {
printf("%d\n",array[i]);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
When I run the code it gives me a read access error. The problem is with the mean() function I created but I don't know what's wrong. Help?
When I run the code it gives me a read access error. The problem is with the mean() function
Although the mean() function produces read access error, the actual problem is here:
printf("%f", mean(i-1, arr[i]));
You are not passing an array to your function, but its element (it is one past the end of what was written, too, so even the value that you pass is undefined).
You need to pass i for the length, because your mean() treats it as an exclusive upper limit, and you also need to pass arr for the array:
printf("%f", mean(i, arr));
Indexing problem when reading the data also needs to be fixed - you need to remove + 1:
scanf_s("%d", &arr[i]);
// ^
Try this:
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { 0 };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf("%d", &n);
getchar();
while (n > 100 || n < 0)
{
printf("Amount of numbers should be more than 0 and less than 100\n");
scanf("%d", &n);
getchar();
}
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
getchar();
}
printf("%lf", mean(n, arr));
getchar();
}
Your call to the mean function was wrong. You have to pass the entire array, not just one element. Change arr[i] to arr.
Other minor modifications are what I did to make it run on my system. If it works for you otherwise, then great.
School project for Computer Science. I need to make a program where the user declares a size for an array, then fills the array in numerical, nondecreasing order, then declares a value, x. X is then assigned to the appropriate spot so the entire array is in numerical, nondecreasing order. The array is then output.
The code builds properly with no errors, but the output is messed up.
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
size++;
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
while(i=0 <= x && x > ary[i]){
i++;
j = size - 1;
while(j >= i) {
ary[j++] = ary[j];
j--;
}
}
for(i = 0; i < size; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main
Example Output:
Enter the size of the array: 5
Enter digits to fill the array, in numerical order: 1
2
3
4
5
Input x, the value to add to the array: 6
1,2,3,4,5,1630076519,
Process returned 0 (0x0) execution time : 8.585 s
Press any key to continue.
It's always the last value that messes up to that huge number. I cannot for the life of me figure out why. The project is due by midnight EST.
For the while loop, can you try this instead,
i = 0;
while (i < x && x > ary[i]) {
i++;
j = size - 1;
while (j >= i) {
j++;
ary[j] = ary[j]; // Equivalent to ary[j++] = ary[j];, yet easier to read
j--;
}
}
Try this:
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
int temp1,temp2;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
for(i=0;i<size;i++)
{
if(ary[i]>x)
{
temp1 = ary[i];
ary[i] = x;
break;
}
}
if(i==size)
{
ary[i]= x;
}
else
{
for(j=i+1;j<size+1;j++)
{
if(j==size) //Last element of the new array
{
ary[j] = temp1;
break;
}
temp2 = ary[j];
ary[j] =temp1;
temp1 = temp2;
}
}
for(i = 0; i < size+1; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main