coding task: array jump back

该博客要求使用C++实现函数“int ArrayChallenge(int arr[], int arrLength)”。函数需先确定数组中最大元素,依据当前索引元素左右移动,可循环数组。若能回到最大元素位置,返回最少跳跃次数;无法回到则返回 -1。还给出了示例输入输出。

requirement :
use c++ to do the function "int ArrayChallenge(int arr[], int arrLength)" :
1. numbers stored in arr[].
2. the function first determine the largest element in the array, 
3. then you can move to left or right in the arr[] with arr[currentindex], you can looping around the arr.
4. continuously according to integer in the current index. 
5. If you can reach the same spot within the array, then the function should return the least amount of jumps it took. 
7. For example: if the input is [2, 3, 5, 6, 1] you'll start at the spot where 6 is and if you jump 6 spaces to the right while looping around the array you end up at the last element where the 1 is. Then from here you can jump 1 space to the left and you're back where you started, so your program should output 2. 
8.If it's impossible to end up back at the largest element in the array your program should output -1. 
9. The largest element in the array will never equal the number of elements in the array. 
10. The largest element is unique.

Examples
Input: {1,2,3,4,2}
Output: 3
Input: {1,7,1,1,1,1}
Output: 2

code :

TBD.

#include <string.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> const char DEFAULT_INPUT_FILE_NAME[] = "im.ppm"; const char OUTPUT_FILE_NAME[] = "im-gray.ppm"; static int read_comment(FILE *file) { char comment[256] = ""; while(fgets(comment, sizeof(comment), file)) { if (comment[strlen(comment) - 1] == '\n') { return 1; } } return 0; } static char *read_next(FILE *file) { int next_char = fgetc(file); char *result_str = malloc(sizeof(char)); int result_str_length = 1; if (result_str == NULL) { return NULL; } result_str[0] = '\0'; while (next_char != EOF) { if (next_char == '#') { read_comment(file); } else if (isspace(next_char) == 0) { result_str_length++; result_str = realloc(result_str, sizeof(char) * result_str_length); if (result_str == NULL) { return NULL; } result_str[result_str_length - 2] = next_char; result_str[result_str_length - 1] = '\0'; } else if (result_str_length != 1) { break; } next_char = fgetc(file); } return result_str; } /* @return -1 if no integer is found */ static int read_next_int(FILE *file) { char *next_str = read_next(file); int result = -1; if (next_str == NULL) { return -1; } for (size_t i = 0; i < strlen(next_str); i++) { if (isdigit(next_str[i]) == 0) { return -1; } } result = atoi(next_str); free(next_str); return result; } static int convert_to_grey(FILE *input, FILE *output, const int max_color_size) { int r = read_next_int(input); int g = read_next_int(input); int b = read_next_int(input); int grey_scale = 0; if (r == -1 || g == -1 || b == -1) { printf("Not an integer\n"); return -1; } if (r > max_color_size || g > max_color_size || b > max_color_size) { printf("An integer is superior to the limit found at the start (%i)\n", max_color_size); return -1; } grey_scale = (r + g + b) / 3; fprintf(output, "%i %i %i\n", grey_scale, grey_scale, grey_scale); return 0; } static int parse_file(FILE *input, FILE *output) { char *file_identifier = read_next(input); int size_x = 0; int size_y = 0; int color_max_size = 0; if (file_identifier == NULL || strcmp(file_identifier, "P3") != 0) { printf("This file is not a PPM file\n"); if (file_identifier != NULL) { free(file_identifier); } return -1; } free(file_identifier); size_x = read_next_int(input); size_y = read_next_int(input); color_max_size = read_next_int(input); if (size_x < -1 || size_y < -1 || color_max_size < -1) { printf("Invalid file specifications\n"); return -1; } fprintf(output, "P3\n%i %i\n%i\n", size_x, size_y, color_max_size); for (int x = 0; x < size_x; x++) { for (int y = 0; y < size_y; y++) { if (convert_to_grey(input, output, color_max_size) == -1) { return -1; } } } return 0; } int main(int argc, char **argv) { char *path = NULL; FILE *input_file = NULL; FILE *output_file = NULL; int result = 0; if (argc <= 1) { path = strdup(DEFAULT_INPUT_FILE_NAME); } else { path = strdup(argv[1]); } if (path == NULL) { printf("Malloc failed\n"); return 500; } input_file = fopen(path, "r"); if (input_file == NULL) { printf("File %s not found\n", path); free(path); return 404; } free(path); output_file = fopen(OUTPUT_FILE_NAME, "w"); if (output_file == NULL) { printf("File %s not found\n", OUTPUT_FILE_NAME); fclose(input_file); return 404; } result = parse_file(input_file, output_file); fclose(input_file); fclose(output_file); if (result != 0) { remove(OUTPUT_FILE_NAME); return 500; } return 0; } Now that you have a complete understanding of the task, do the following: • Identify and analyse the bottlenecks of your serial implementation using necessary profiling tools. (15 points), and • provide a design of a solution to speed up using parallel programming. You do not need to do the actual coding for the parallel implementation, provide the detailed design using 4-step Forster Method with maximum 500 words, (20 points) and • a figure to explain your design, the figure should align with the parallel design steps and should be clear and easy to understand. (10 points)
10-27
【激光质量检测】利用丝杆与步进电机的组合装置带动光源的移动,完成对光源使用切片法测量其光束质量的目的研究(Matlab代码实现)内容概要:本文研究了利用丝杆与步进电机的组合装置带动光源移动,结合切片法实现对激光光源光束质量的精确测量方法,并提供了基于Matlab的代码实现方案。该系统通过机械装置精确控制光源位置,采集不同截面的光强分布数据,进而分析光束的聚焦特性、发散角、光斑尺寸等关键质量参数,适用于高精度光学检测场景。研究重点在于硬件控制与图像处理算法的协同设计,实现了自动化、高重复性的光束质量评估流程。; 适合人群:具备一定光学基础知识和Matlab编程能力的科研人员或工程技术人员,尤其适合从事激光应用、光电检测、精密仪器开发等相关领域的研究生及研发工程师。; 使用场景及目标:①实现对连续或脉冲激光器输出光束的质量评估;②为激光加工、医疗激光、通信激光等应用场景提供可靠的光束分析手段;③通过Matlab仿真与实际控制对接,验证切片法测量方案的有效性与精度。; 阅读建议:建议读者结合机械控制原理与光学测量理论同步理解文档内容,重点关注步进电机控制逻辑与切片数据处理算法的衔接部分,实际应用时需校准装置并优化采样间距以提高测量精度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值