C Program Help: Unexpected Output - c

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

Related

How can I move all elements in an array with a K number and make the numbers rotate when it comes to the end?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}

Finding pairs in an array that are equal to an input value

I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}

Having trouble rearranging first array in accordance to second array

I am trying to rearrange my first array (vector1) in accordance to the sequence of the indices second array (vector2)
For example, if:
vector1 = 1 2 3 4
and vector2 = 0 3 1 2
my desired output should be:
1 4 2 3
However, the execution of my code is continuously interrupted due to using an "illegal array index." Would someone be able to point out where I am going wrong? Thanks so much in advance.
#include <stdio.h>
#include <math.h>
int main(void) {
int n;
printf("Enter vector length: ");
scanf("%d", &n);
printf("Enter vector: ");
int vector1[n];
int i = 0;
while (i < n) {
scanf("%d", &vector1[i]);
i++;
}
printf("Enter permutation: ");
int vector2[n];
i = 0;
int j = 0;
while (i < n) {
scanf("%d", &vector2[j]);
i++;
}
i = 0;
int vector3[n];
while (i < n) {
vector3[i] = vector1[vector2[i]];
i++;
}
printf("%d", vector3[i]);
}
While using vector2 you are getting the value of vector2[j] but increasing the value of i. You have assigned j=0 above so the loop is scanning the same value again and again i.e vector2[0] until the loop goes off. So to correct your program either you increase j in your loop by replacing j++ instead of i++ or you can just replace vector2[j] with vector2[i] in the loop.
#include <stdio.h>
#include <math.h>
int main(void)
{
int n;
printf("Enter vector length: ");
scanf("%d", &n);
printf("Enter vector: ");
int vector1[n];
int i = 0;
while (i < n)
{
scanf("%d", &vector1[i]);
i++;
}
printf("Enter permutation: ");
int vector2[n];
i = 0;
while (i < n)
{
scanf("%d", &vector2[i]);
i++;
}
i = 0;
int vector3[n];
while (i < n)
{
vector3[i] = vector1[vector2[i]];
i++;
}
for(i=0;i<n;i++)
printf("%d", vector3[i]);
}

Frequency of an Element Accruing In an Array

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.

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

Resources