Answer to Question #225480 in C for shravan

Question #225480

Victor has an array of size n. He loves to play with these n numbers. Each time he plays

with them, he picks up any two consecutive numbers and adds them. On adding both the

numbers, it costs him k*(sum of both numbers).

Find the minimum cost of adding all the numbers in the array.

Input Specification:

input1: Size of array.

input2: Elements of the array.

input3: Value of k.

Output Specification:

Return the minimum cost of adding all the numbers of the array.


1
Expert's answer
2021-08-12T16:23:50-0400

//Answer

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define MAXSIZE 6555
int main() {


	int n;//size of array
	scanf("%i", &n);
	int a[MAXSIZE];
	for (int i = 0; i < n; i++)
		scanf("%i", &a[i]);
	int minCost=INT_MAX;
	int k;
	scanf("%i", &k);
	if (n == 1)
		return printf("One elements: minimum cost=%i\n", a[0]),0;
	for (int i = 0; i < n - 1; i++)
	{
		if (k * (a[i] + a[i + 1]) < minCost)
			minCost = k * (a[i] + a[i + 1]);
	}
	printf("Minimum cost: = %i\n", minCost);
	return 0;
}
/*
Example test
Array size 4
[1,2,3,4]
k-3
answer=9


3*(1+2)=9
3*(2+3)=15
........
Minimum=9
*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS