I wrote this program to calculate the scalar product and find out the smallest as well as the biggest number (+ there position) of the vector v. Everything works fine except for the lowest_number function. It find the lowest number inside the vector v and the position as well,but when the position is at v[0] ----> at 1 it displays a really long number. I am not sure why this happens. PLease help thank you very much
int scalar_produc(int *v,int *w,int n)
{
int i = 0;
int sp;
int sp2 = 0;
for(i= 0; i < n; i++)
{
sp = v[i] * w[i];
sp2 = sp2 + sp;
}
return sp2;
}
int lowest_number(int *v,int n)
{
int i = 0;
int low = v[i];
int position;
for(i = 0; i < n; i++)
{
if(v[i] < low)
{
low = v[i];
position = i;
}
}
printf("The lowest number is: %d \n", low);
printf("The position of it: %d \n",position+1);
}
int biggest_number(int *v, int n)
{
int i = 0;
int position;
int biggest = v[i];
for(i = 0; i < n; i++)
{
if(v[i] > biggest)
{
biggest = v[i];
position = i;
}
}
printf("The biggest number of the vector 'v' is: %d \n",biggest);
printf("The position of the biggest number: %d \n", position+1);
}
int main()
{
int n,i;
int *v,*w;
printf("Enter the number of vectors you would like to enter: \n");
scanf("%d",&n);
v = (int*) malloc(sizeof(int) *n);
w = (int*) malloc(sizeof(int) *n);
printf("Enter the vectors: \n");
for(i = 0; i < n ;i++)
{
printf("v[%d]: \n",i+1);
scanf("%d",&v[i]);
printf("w[%d]: \n",i+1);
scanf("%d",&w[i]);
}
printf("The scalar product: %d \n",scalar_produc(v,w,n));
lowest_number(v,n);
biggest_number(v,n);
free(v);
free(w);
return 0;
}
You don't initialize position. So if the smallest value is the first value, the value of position is undefined.
Initialize it to 0 and it should work.
int i = 0;
int low = v[i];
int position = 0;
Related
I want to search for the lowest subtotal of k values in an array z with n elements, filled by user input. This works fine with k==1 but I do not understand why my code does not work for k>1.
My program outputs the wrong index and subtotal.
The function is being called from main, after filling the array with n elements of int values in a loop until it's filled, asking for the desired length of k.
Please help me understand what I'm doing wrong
#include <stdio.h>
void lowest_subtotal(int z[], int n, int k){
int t[]={0};
int i = 0, position = 0, tempmin;
if(k==1){ //working 100%
while(i<n){
if(z[i]<tempmin){
tempmin=z[i];
position=i;
}
i++;
}
printf("Lowest subtotal on Index %d. (sum: %d.)", position, tempmin);
}
else{
//calculate subtotals
while(i<=n){
for(int j=0; j<k; j++){
if(z[i+j]<0){
t[i] -= z[j];
}
else{
t[i] += z[j];
}
}
i++;
}
//search for lowest subtotal
i = 0;
while(i<n-k+1){
if(t[i] < tempmin){
tempmin = t[i];
position=i;
}
i++;
}
//wrong result of index and sum?
printf("Lowest subtotal on Index %d. (sum: %d.)",position-k-1,tempmin);
}
}
int main(){
int n,k; //length of vector, length of subtotal
printf("n: ");
scanf("%d",&n);
int z[99] = { 0 }; //initialize array
printf("input elements:\n");
for(int i=0; i<n; i++){
scanf("%d",&z[i]);
}
printf("length of subtotal?: ");
scanf("%d",&k);
lowest_subtotal(z,n,k);
}
It will be much easier for you if you split it into separate tasks (functions.
long long subtotal(int *arr, size_t size, size_t start, size_t k)
{
long long result = 0;
for(size_t index = 0; index < k; index ++)
{
result += arr[start + index];
}
return result;
}
long long smallestSubtotal(int *arr, size_t size, size_t k, size_t *startpos)
{
long long result = LLONG_MAX;
for(size_t start = 0; start < size - k; start++)
{
long long sum = subtotal(arr, size, start, k);
if(result > sum)
{
if(startpos) *startpos = start;
result = sum;
}
}
return result;
}
void lowest_subtotal(int z[], size_t n, size_t k)
{
size_t index;
long long subt;
subt = smallestSubtotal(z, n, k, &index);
printf("The smallest subtotal of length %zu is %lld at index %zu\n", k, subt, index);
}
int main(void)
{
int arr[SIZE];
srand(time(NULL));
for(size_t index = 0; index < SIZE; index++)
{
arr[index] = rand();
}
lowest_subtotal(arr, SIZE, 15);
}
As I understand from the OP code, the task is to find the starting index of k consecutive numbers in an array of n numbers such that the sum of the k numbers is minimal.
The case of k=1 need not be a special case. Besides, the idea of updating the sum of a "window" of k elements is good, but there are several errors in the implementation. A working version of lowest_subtotal() could be as follows:
#include<limits.h>
int lowest_subtotal(int z[], int n, int k)
{
if(k > n || k < 1)
{
return -1; // Error, k must be in the range from 1 to n
}
// Calculate sum in window at index 0
int sum = 0;
for(int i = 0; i < k; i++)
{
sum += z[i];
}
// Slide window and update sum
int index = 0;
int min_sum = sum;
for(int i = 1; i < n - k; i++)
{
sum += z[i+k-1] - z[i-1]; // Add incoming value and subtract the leaving value
if(sum < min_sum)
{
min_sum = sum;
index = i;
}
}
printf("Lowest subtotal at index %d (sum=%d)\n", index, min_sum);
return index;
}
I'm creating a program that reads input from an array and orders it in an ascending order. However, I also wanted to count the number of times each element appears in the array, but I'm struggling to to this. This is the code I have so far:
#include <stdlib.h>
#include <locale.h>
typedef enum _ordem {
Crescente=1
} ordem;
void troca(int *x, int *y){
int wk;
wk=*x;*x=*y;*y=wk;
}
int trocar(int x, int y, ordem dir){
if(dir == Crescente)
return x > y;
return 0;
}
void bubbleSort(int *array, int top, int fim, ordem dir){
int i, j, trocado;
for(i = top; i < fim; ++i){
trocado = 0;
for(j = top + 1; j <= fim - i; ++j)
if(trocar(array[j-1], array[j], dir)){
troca(&array[j-1], &array[j]);
trocado = 1;
}
if(trocado == 0)break;
}
}
int main(){
int vetor[100], index, ordem=1;
index=0;
setlocale(LC_ALL,"Portuguese");
printf("Introduza os números. Escreva -00 para parar. \n");
do{
scanf("%d", &vetor[index++]);
}while(vetor[index-1] != -00 && index < 100);
--index;
bubbleSort(vetor, 0, index-1, ordem);
{
int i;
for(i=0;i<index;++i)
printf("%d ", vetor[i]);
printf("\n");
}
return 0;
}
After the numbers have been sorted, equal numbers will be next to each other in the array. This makes it easy to count the number of times each number occurs using a single loop that iterates through the sorted array, with an extra variable that is incremented when the next element has the same number as the current element, or is reset when the next element has a different number.
The following block of code will do that:
{
int i, c;
c = 1;
for (i = 0; i < index; ++i) {
if (i < index - 1 && vetor[i] == vetor[i + 1]) {
c++;
} else {
printf("%d(x%d) ", vetor[i], c);
c = 1;
}
}
printf("\n");
}
You could create another array to store a table of the unique values with their frequency of occurrance:
struct freq {
int val;
int freq;
};
struct freq freq[100];
int nfreq = 0;
Fill the table using a loop similar to the earlier code:
{
int i, c;
c = 1;
for (i = 0; i < index; ++i) {
if (i < index - 1 && vetor[i] == vetor[i + 1]) {
c++;
} else {
freq[nfreq].val = vetor[i];
freq[nfreq].freq = c;
nfreq++;
c = 1;
}
}
}
Print the out the table with another simple loop:
{
int i;
for (i = 0; i < nfreq; i++) {
printf("%d(x%d) ", freq[i].val, freq[i].freq);
}
printf("\n");
}
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;
}
I want to write a program that reads 10 int values from the user and swaps the largest and smallest numbers on the first and second values, then the rest of the numbers should be in the order.
Please check the code and help me what the wrong is.
For instance:
1
9
4
5
6
7
8
2
4
5
New order should be 9 1 4 5 6 7 8 2 4 5
#include <stdio.h>
int main() {
int a[10],i,min,max=0,pos=0;
printf("Please enter 10 int values :\n");
do{
scanf("%d", &a[pos++]);
} while (pos<10);
for (i=0; i<10;i++) {
printf("%i\n",a[i]);
if (max<a[i])
{
max=a[i];
}
if (min>a[i])
{
min=a[i];
}
for (i=0;i<10;i++) {
if (a[i]==max)
a[i]=max;
if (a[i] == min) a[i] = min;
}
printf("The new order is : %d %d %d ", max, min, ...);
return 0;
}
EDIT:
It is the new form
#include <stdio.h>
int main() {
int a[10],i,pos,temp,min = 0,max = 0;
printf("Please enter 10 int values :\n");
do {
scanf("%d", &a[pos++]);
} while (pos < 10);
for ( =1; i<10;i++) {
if (a[i]>a[max])
{
max=i;
}
if (a[i]<a[min])
{
min=i;
}
}
temp=a[max];
a[max]=a[min];
a[min]=temp;
printf("%d %d",a[max],a[min]);
for (i=0;i<10;i++){
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
return 0;
}
As others have noted, your code does not properly identify the maximum and minimum values in the array because you are writing min and max back into the array instead of the other way around.
Since you want to swap these values, what you actually want are the indices of the min and max values of the array, and swap those.
It is best to break this code into functions instead of having everything in main. Here is a solution that will do what you want:
#include <stdio.h>
int indexofmax(int *data, int len)
{
int max = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]>data[max]) max = i;
}
return max;
}
int indexofmin(int *data, int len)
{
int min = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]<data[min]) min = i;
}
return min;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
// user enters in 10 ints...
int max = indexofmax(a, 10);
int min = indexofmin(a, 10);
int i;
swap(&a[min], &a[max]);
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
This initialization min=0,max=0 is not right.
Instead have min = INT_MAX and max = INT_MIN.
By setting min=0, you would never get the lowest number in the array if it is greater than 0.
Similarly by setting max=0, you would never get the greatest number in the array if it is lower than 0.
You are gaining nothing by this code:
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
It is evident that this loop
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
does not make sense.
Moreover variable min is not initialized while variable max is initialized incorrectly.
int a[10],i,min,max=0,pos=0;
For example the array can contain all negative elements. In this case you will get incorrect value of the maximum equal to 0.
And I do not see where the elements are moved to the right to place the maximum and the minimum to the first two positions of the array.
If I have understood correctly then what you need is something like the following. To move the elements you could use standard function memmove declared in header <string.h>. However it seems you are learning loops.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 4, 5, 9, 6, 7, 1, 8, 2, 4, 5 };
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
size_t min = 0;
size_t max = 0;
for (size_t i = 1; i < N; i++)
{
if (a[max] < a[i])
{
max = i;
}
else if (a[i] < a[min])
{
min = i;
}
}
if (max != min)
{
int min_value = a[min];
int max_value = a[max];
size_t j = N;
for (size_t i = N; i != 0; --i)
{
if (i - 1 != min && i - 1 != max)
{
if (i != j)
{
a[j - 1] = a[i - 1];
}
--j;
}
}
a[--j] = min_value;
a[--j] = max_value;
}
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
}
The program output is
4 5 9 6 7 1 8 2 4 5
9 1 4 5 6 7 8 2 4 5
You're not actually altering the array.
In the second loop, you say "if the current element is the max, set it to the max". In other words, set it to its current value. Similarly for the min.
What you want is to swap those assignments.
if(a[i]==max) a[i]=min;
if(a[i]==min) a[i]=max;
Also, your initial values for min and max are no good. min is unitialized, so its initial value is undefined. You should initialize min to a very large value, and similarly max should be initialized to a very small (i.e. large negative) value.
A better way to do this would be to keep track of the index of the largest and smallest values. These you can initialize to 0. Then you can check a[i] > a[max] and a[i] < a[min]. Then you print the values at indexes min and max, then loop through the list and print the others.
int i, temp, min=0, max=0;
for (i=1; i<10; i++) {
if (a[i] > a[max]) max = i;
if (a[i] < a[min]) min = i;
}
printf("%d %d ", a[max], a[min]);
for (i=0; i<10; i++) {
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
Just keep it nice and simple, like this:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 10
int find_biggest(int A[], size_t n);
int find_smallest(int A[], size_t n);
void print_array(int A[], size_t n);
void int_swap(int *a, int *b);
int
main(void) {
int array[MAXNUM], i, smallest, biggest;
printf("Please enter 10 int values:\n");
for (i = 0; i < MAXNUM; i++) {
if (scanf("%d", &array[i]) != 1) {
printf("invalid input\n");
exit(EXIT_FAILURE);
}
}
printf("Before: ");
print_array(array, MAXNUM);
smallest = find_smallest(array, MAXNUM);
biggest = find_biggest(array, MAXNUM);
int_swap(&array[smallest], &array[biggest]);
printf("After: ");
print_array(array, MAXNUM);
return 0;
}
int
find_biggest(int A[], size_t n) {
int biggest, i, idx_loc;
biggest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] > biggest) {
biggest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
int
find_smallest(int A[], size_t n) {
int smallest, i, idx_loc;
smallest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] < smallest) {
smallest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
void
print_array(int A[], size_t n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
void
int_swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Hey everyone so my MergeSort algorithm isn't perfect, it's actually the same one that was posted here in a similar question, but that's not even the real problem.
Basically the user inputs an array size, and lower & upper bounds on a range of values to be put into an array of doubles to be mergeSorted.
I can print out the array just fine but after MergeSort it returns whack negative values that I assume are addresses.
I know this has something to do with memory but I have no idea why it's messing up because I believe I allocated memory and cleared it correctly. Here is my code, any insight will be much appreciated especially before 12:00 tonight ;).
Here is main.c, mergesort.c, and mergesort.h. I am also using a makefile but I don't think that is necessary to share.
Main.c:
#include <stdlib.h>
#include <stdio.h>
#include "mergesort.h"
int lower = 0;
int upper = 0;
int n = 0;
int main(int argc,char* argv[]) {
int i = 0; // loop values
int j = 0;
int r = 0; // random number
/*int n = 0; // size of array
int lower = 0; // lower bound on values in array
int upper = 0; // upper ...*/
double *t; //array of doubles
printf("Size of array?\n");
scanf("%d", &n);
printf("Lower Bound?\n");
scanf("%d", &lower);
//globLower = lower;
printf("Upper Bound?\n");
scanf("%d", &upper);
//globUpper = upper;
t = malloc(n * sizeof *t); // allocates n slots of memory
printf("Unsorted Array:\n");
for(i=0; i<n; i++) {
r = lower + arc4random() % (upper - lower);
//printf("%d\n", r);
t[i] = r; // fills array with random values in range
printf("%g\n", t[i]);
}
printf("Before Merge Sort\n");
mergeSort(t,n); // This is supposed to sort the array...
printf("After Merge Sort\n");
i = 0; //reset
// printf("%g\n", t[0]);
printf("%g\n", t[1]);
printf("%g\n", t[2]);
free(t); // Need to free the memory allocated since is stored in the heap
return 0;
}
mergesort.c:
#include <stdlib.h>
#include <stdio.h>
#include "mergesort.h"
void mergeSort(double* t, int n) {
printf("Size: %d\n", n);
printf("Lower: %d\n", lower); // tests
printf("Upper: %d\n", upper);
printf("t[2]: %g\n", t[2]);
int beg = 0;
int end = n-1;
mergeSortHelp(t, beg, end);
}
void mergeSortHelp(double* t, int beg, int end) {
int mid = (end + beg) / 2;
if(beg < end) {
mergeSortHelp(t, beg, mid);
mergeSortHelp(t, mid+1, end);
merge(t, beg, mid, end);
}
}
void merge(double* t, int beg, int mid, int end) {
int sizeLeft = mid - beg + 1;
int sizeRight = end - mid;
double *left = malloc((sizeLeft)*sizeof(double));
double *right = malloc((sizeRight)*sizeof(double));
int i,j,k;
for(i = 0; i < sizeLeft; i++) {
left[i] = t[beg+i];
}
for(j = 0; j < sizeRight; j++) {
right[i] = t[beg+j];
}
i = 0;
j = 0;
for(k = beg; k <= end; k++) {
t[k] = (left[i] <= right[j]) ? left[i++] : right[j++];
}
free(left);
free(right);
return;
}
and the header mergesort.h
extern int lower;
extern int upper;
extern int n;
Thanks!
It looks like you may have two bugs here:
for(j = 0; j < sizeRight; j++) {
right[i] = t[beg+j];
This should probably be:
for(j = 0; j < sizeRight; j++) {
right[j] = t[med+j];
^^^ ^^^